Java Examples - Get Thread State
In the lifecycle of a Java thread, the Thread class has an enumeration type State that defines several thread states, namely:
- New
- Runnable
- Blocked
- Waiting
- Timed Waiting
- Terminated
Description of each state:
1. Initial State - NEW
Declaration:
public static final Thread.State NEW
Implementing the Runnable interface and extending Thread can produce a thread class. When an instance is created with new, the thread enters the initial state.
2. RUNNABLE
Declaration:
public static final Thread.State RUNNABLE
2.1. Ready State
The ready state simply means you are qualified to run. If the scheduler does not pick you, you will always be in the ready state.
Calling the thread's start() method puts the thread into the ready state.
When the current thread's sleep() method ends, another thread's join() ends, waiting for user input finishes, or a thread obtains the object lock, these threads will also enter the ready state.
When the current thread has used up its time slice, or the yield() method is called on the current thread, the current thread enters the ready state.
After a thread in the lock pool obtains the object lock, it enters the ready state.
2.2. Running State
The state a thread is in when the thread scheduler selects it from the runnable pool as the current thread. This is also the only way a thread enters the running state.
3. Blocked State - BLOCKED
Declaration:
public static final Thread.State BLOCKED
The blocked state is the state when a thread is blocked while entering a method or code block modified by the synchronized keyword (acquiring a lock).
4. Waiting - WAITING
Declaration:
public static final Thread.State WAITING
Threads in this state will not be allocated CPU execution time. They must wait to be explicitly awakened, otherwise they will be in a state of indefinite waiting.
5. Timed Waiting - TIMED_WAITING
Declaration:
public static final Thread.State TIMED_WAITING
Threads in this state are not allocated CPU execution time, but they do not need to wait indefinitely to be explicitly awakened by other threads; after a certain time they will wake up automatically.
6. Terminated State - TERMINATED
Declaration:
public static final Thread.State TERMINATED
When a thread's run() method completes, or the main thread's main() method completes, we consider it terminated. The thread object may be alive, but it is no longer a separate executing thread. Once a thread is terminated, it cannot be resurrected.
Calling the start() method on a terminated thread throws a java.lang.IllegalThreadStateException.
The following example demonstrates how to get a thread's state:
Main.java file
The output of the above code is:
State of thread1 after creating it - NEW State of thread1 after calling .start() method on it - RUNNABLE State of thread2 after creating it - NEW State of thread2 after calling .start() method on it - RUNNABLE State of thread2 after calling .sleep() method on it - TIMED_WAITING State of thread1 while it called join() method on thread2 -WAITING State of thread2 when it has finished it's execution - TERMINATEDOther Extensions
Java Examples