Thread.currentThread() : Thread « java.lang « Java by API






Thread.currentThread()

  
/*
 * Output: 

currentThread: Thread[main,5,main]
Thread created: Thread[Demo Thread,5,main]
5
4
3
2
1
exiting child thread


 */

public class MainClass implements Runnable {
  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();

  }

  public void run() {
    for (int i = 5; i > 0; i--) {
      System.out.println("" + i);
    }
    System.out.println("exiting child thread");
  }

  public static void main(String args[]) {
    new MainClass();
  }
}
           
         
    
  








Related examples in the same category

1.Thread.MAX_PRIORITY
2.Thread.NORM_PRIORITY
3.new Thread(Runnable target, String name)
4.Thread.activeCount()
5.Thread.dumpStack()
6.Thread.enumerate(Thread[] tarray)
7.Thread: getStackTrace()
8.Thread: getThreadGroup()
9.Thread: getUncaughtExceptionHandler()
10.Thread: interrupt()
11.Thread: isAlive()
12.Thread: isDaemon()
13.Thread: join() (Using join() to wait for threads to finish)
14.Thread: run()
15.Thread: setDaemon(boolean b)
16.Thread: setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
17.Threah.sleep(long millis)
18.Thread: setDaemon(boolean on)
19.Thread: setName(String name)
20.Thread: setPriority(int newPriority)
21.Thread: setUncaughtExceptionHandler(UncaughtExceptionHandler eh)
22.Thread: start
23.Thread: stop
24.implements UncaughtExceptionHandler