|
A thread is a thread of execution in a program. The JVM allows the multiple threads to run concurrently. The java.lang.Thread class has the following enhancements in 5.0:
- The priority handling has changed for the Thread.
- One enum class i.e. Thread.State is introduced and the new getState() API are provided for querying the execution state of a thread.
- getStackTrace and getAllStackTraces methods added in the Thread class to obtain the stack trace of a thread or all threads.
- The uncaughtExceptionHandler mechanism is also introduced in the Thread class.
- A new overloaded sleep() method is provided to allow the Thread to sleep smaller than one millisecond.
Example:-
A simple thread example to show the new method.
package com.visualbuilder;
public class ThreadExample extends Thread{ Thread t; ThreadExample(){ t= new Thread(this); t.start(); } public void run() { System.out.println(t.getState().toString()); System.out.println(t.getAllStackTraces()); } public static void main(String[] args) { new ThreadExample();
}
}
Output:-
The following is the output of the program.
RUNNABLE {Thread[Attach Listener,5,system]=[Ljava.lang.StackTraceElement;@1fb8ee3, Thread[Reference Handler,10,system]=[Ljava.lang.StackTraceElement;@61de33, Thread[Thread-1,5,main]=[Ljava.lang.StackTraceElement;@14318bb, Thread[Finalizer,8,system]=[Ljava.lang.StackTraceElement;@ca0b6, Thread[Signal Dispatcher,9,system]=[Ljava.lang.StackTraceElement;@10b30a7, Thread[DestroyJavaVM,5,main]=[Ljava.lang.StackTraceElement;@1a758cb} |