A thread is always in one of the following six JVM states:
When a thread is created and its start() method is not yet called, it is in the new state.
Thread t = new SomeThreadClass(); // t is in the new state
A thread that is ready to run or running is in the runnable state. A thread that is eligible for getting the CPU time is in a runnable state.
A thread is in blocked state if it was trying to enter a synchronized method but the monitor is being used by another thread.
A thread may place itself in a waiting state by calling one of the methods in the following table.
Method | Description |
---|---|
wait() | wait() method from the Object class. a thread must own the monitor's lock of an object to call the wait() method on that object. Another thread must call the notify() or notifyAll() method on the same object in order for the waiting thread to transition to the runnable state. |
join() | join() method from the Thread class. A thread that calls this method wants to wait until the thread on which this method is called terminates. |
park() | park() method from the LockSupport class. A thread that calls this method may wait until a permit is available by calling the unpark() method on a thread. |
A thread may place itself in a timed-waiting state by calling one of the methods in the following table.
Method | Description |
---|---|
sleep() | This method is in the Thread class. |
wait (long millis) wait(long millis, int nanos) | These methods are in the Object class. |
join(long millis) join(long millis, int nanos) | These methods are in the Thread class. |
parkNanos (long nanos) parkNanos (Object blocker, long nanos) | These methods are in the LockSupport class, which is in the java.util.concurrent.locks package. |
parkUntil (long deadline) parkUntil (Object blocker, long nanos) | These methods are in the LockSupport class, which is in the java.util.concurrent.locks package. |
A thread completed its execution is in the terminated state.
A thread is terminated when it exits its run() method or its stop() method is called.
A terminated thread cannot transition to any other state.
isAlive() method from a thread tells if it is alive or terminated.