Example usage for java.lang Thread currentThread

List of usage examples for java.lang Thread currentThread

Introduction

In this page you can find the example usage for java.lang Thread currentThread.

Prototype

@HotSpotIntrinsicCandidate
public static native Thread currentThread();

Source Link

Document

Returns a reference to the currently executing thread object.

Usage

From source file:Main.java

public Main() {
    String name = Thread.currentThread().getName();
    System.out.println("Main() constructor: " + name);
}

From source file:Main.java

public void doit() {
    StackTraceElement e = Thread.currentThread().getStackTrace()[3];
    System.out.println(e.getMethodName());
}

From source file:AlternateStop.java

public void run() {
    runThread = Thread.currentThread();
    stopRequested = false;/*from   ww  w .ja v  a 2 s.c  om*/

    int count = 0;

    while (!stopRequested) {
        System.out.println("Running ... count=" + count);
        count++;

        try {
            Thread.sleep(300);
        } catch (InterruptedException x) {
            // re-assert interrupt
            Thread.currentThread().interrupt();
        }
    }
}

From source file:me.j360.dubbo.modules.util.concurrent.ThreadUtil.java

/**
 * sleep???InterruptedException.//from  w w w.java  2 s.  c  o m
 */
public static void sleep(long duration, TimeUnit unit) {
    try {
        Thread.sleep(unit.toMillis(duration));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public void printMyName() {
    System.out.println("The Thread name is " + Thread.currentThread().getName());
}

From source file:DaemonThread.java

public void run() {
    System.out.println("entering run()");

    try {//from   w  w  w. j av a  2  s .  c  o m
        System.out.println("in run(): currentThread() is" + Thread.currentThread());

        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException x) {
            }

            System.out.println("in run(): woke up again");
        }
    } finally {
        System.out.println("leaving run()");
    }
}

From source file:Main.java

/**
 * Uses the shutdown pattern from http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
 * @param pool ExecutorService need to shutdown
 * @param period wait time period/*from   ww w.j  av a2  s  .co  m*/
 * @param unit wait time unit.
 */
public static void shutdownAndAwaitTermination(ExecutorService pool, long period, TimeUnit unit) {
    pool.shutdown(); // Disable new tasks from being submitted
    try {
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(period, unit)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!pool.awaitTermination(period, unit))
                System.err.println("Pool did not terminate");
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static final void shutdownAndAwaitTermination(ExecutorService executorService, long timeout,
        TimeUnit timeUnit) {/*from   w  w  w.java  2s .co  m*/
    if (executorService.isShutdown()) {
        return;
    }
    executorService.shutdown();
    try {
        if (!executorService.awaitTermination(timeout, timeUnit)) {
            executorService.shutdownNow();
        }
    } catch (InterruptedException ie) {
        executorService.shutdownNow();
        Thread.currentThread().interrupt();
    }
    executorService.shutdownNow();

}

From source file:MainClass.java

MainClass() {
    Thread ct = Thread.currentThread();
    Thread t = new Thread(this, "Demo Thread");
    System.out.println("currentThread: " + ct);
    System.out.println("Thread created: " + t);
    t.start();/*from www.ja  va2  s .  com*/

}

From source file:Main.java

/**
 * Puts the current thread to sleep for the specified amount of time.
 *
 * @param sleepTime//w ww  .  j  a v a  2  s. com
 *        in milliseconds
 */
public static void sleep(long sleepTime) {

    Thread currentThread = Thread.currentThread();

    try {

        Thread.sleep(sleepTime);

    } catch (InterruptedException e) {

        e.printStackTrace();
        currentThread.interrupt();
    }
}