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:CurrentThreadDemo.java

public static void main(String args[]) {
    Thread t = Thread.currentThread();

    System.out.println("Current thread: " + t);

    t.setName("My Thread");
    System.out.println("After name change: " + t);

    try {//from   w  w  w.  j av  a 2 s. c  o  m
        for (int n = 5; n > 0; n--) {
            System.out.println(n);
            Thread.sleep(1000);
        }
    } catch (InterruptedException e) {
        System.out.println("Main thread interrupted");
    }
}

From source file:PendingInterrupt.java

public static void main(String[] args) {
    Thread.currentThread().interrupt();

    long startTime = System.currentTimeMillis();
    try {//from   w w  w  .ja va2s  . c  om
        Thread.sleep(2000);
        System.out.println("NOT interrupted");
    } catch (InterruptedException x) {
        System.out.println("Interrupted");
    }

    System.out.println("elapsedTime=" + (System.currentTimeMillis() - startTime));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ThreadGroup group = Thread.currentThread().getThreadGroup().getParent();
    while (group.getParent() != null) {
        group = group.getParent();//ww w . ja v  a2 s  . co m
    }
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);

    for (int i = 0; i < numThreads; i++) {
        Thread thread = threads[i];
        System.out.println(thread.getName());
    }

    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);

    for (int i = 0; i < numGroups; i++) {
        System.out.println(groups[i]);
    }
}

From source file:Main.java

public static void main(String args[]) {

    new ThreadClass("A");
    Thread t = Thread.currentThread();

    try {/*from   w  w w  . j  a v a2 s.c o m*/
        t.checkAccess();
        System.out.println("You have permission to modify");
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) {

    System.out.println("Is Native method ? :");

    Thread t = Thread.currentThread();
    StackTraceElement e = t.getStackTrace()[0];

    System.out.println(e.isNativeMethod());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();

    MyThread mt1 = new MyThread(tg, "first");
    MyThread mt2 = new MyThread(tg, "second");

    mt1.start();//from   w  w w .j av  a2  s.  c o  m
    mt2.start();

    ThreadGroup parent = tg.getParent();
    Thread[] list = new Thread[parent.activeCount()];
    int count = parent.enumerate(list, true);
    String[] thdinfo = new String[count];
    for (int i = 0; i < count; i++)
        thdinfo[i] = list[i].toString();

    mt1.join();
    mt2.join();

    for (int i = 0; i < count; i++)
        System.out.println(thdinfo[i]);
}

From source file:Main.java

public static void main(String[] args) {
    Thread t = new Thread(Main::run);
    t.start();/*from  ww w .ja v a 2 s . c  o  m*/
    try {
        Thread.currentThread().sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    t.interrupt();
}

From source file:Main.java

public static void main(String[] args) {
    System.out.println("#1:" + Thread.interrupted());

    Thread mainThread = Thread.currentThread();
    mainThread.interrupt();// w w  w.j  a va 2  s. c o m

    System.out.println("#2:" + mainThread.isInterrupted());

    System.out.println("#3:" + mainThread.isInterrupted());

    System.out.println("#4:" + Thread.interrupted());

    System.out.println("#5:" + mainThread.isInterrupted());
}

From source file:InterruptReset.java

public static void main(String[] args) {
    System.out.println("Thread.interrupted()=" + Thread.interrupted());
    Thread.currentThread().interrupt();
    System.out.println("Thread.interrupted()=" + Thread.interrupted());
    System.out.println("Thread.interrupted()=" + Thread.interrupted());
}

From source file:Main.java

public static void main(String[] args) {
    System.out.println("#1:" + Thread.interrupted());

    // Now interrupt the main thread
    Thread.currentThread().interrupt();

    // Check if it has been interrupted
    System.out.println("#2:" + Thread.interrupted());

    // Check again if it has been interrupted
    System.out.println("#3:" + Thread.interrupted());
}