A <i>thread</i> is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. <p> Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new {@code Thread} object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon. <p> When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named {@code main} of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: <ul> <li>The {@code exit} method of class {@code Runtime} has been called and the security manager has permitted the exit operation to take place. <li>All threads that are not daemon threads have died, either by returning from the call to the {@code run} method or by throwing an exception that propagates beyond the {@code run} method. </ul> <p> There are two ways to create a new thread of execution. One is to declare a class to be a subclass of {@code Thread}. This subclass should override the {@code run} method of class {@code Thread}. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows: <hr><blockquote><pre> class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; }
A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent. <p> A thread is allowed to access information about its own thread group, but not to access information about its thread group's parent thread group or any other thread groups.
A thread state. A thread can be in one of the following states: <ul> <li>{@link #NEW}<br> A thread that has not yet started is in this state. </li> <li>{@link #RUNNABLE}<br> A thread executing in the Java virtual machine is in this state. </li> <li>{@link #BLOCKED}<br> A thread that is blocked waiting for a monitor lock is in this state. </li> <li>{@link #WAITING}<br> A thread that is waiting indefinitely for another thread to perform a particular action is in this state. </li> <li>{@link #TIMED_WAITING}<br> A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. </li> <li>{@link #TERMINATED}<br> A thread that has exited is in this state. </li> </ul>
Interface for handlers invoked when a {@code Thread} abruptly terminates due to an uncaught exception. <p>When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its {@code UncaughtExceptionHandler} using {@link #getUncaughtExceptionHandler} and will invoke the handler's {@code uncaughtException} method, passing the thread and the exception as arguments. If a thread has not had its {@code UncaughtExceptionHandler} explicitly set, then its {@code ThreadGroupEx} object acts as its {@code UncaughtExceptionHandler}. If the {@code ThreadGroupEx} object has no special requirements for dealing with the exception, it can forward the invocation to the {@linkplain #getDefaultUncaughtExceptionHandler default uncaught exception handler}.