Hello Everyone,
In Java, to get the current state of the thread, use Thread.getState() method to get the current state of the thread. Java provides java.lang.Thread.State class that defines the ENUM constants for the state of a thread, as summary of which is given below:
- Constant type: New
Declaration: public static final Thread.State NEW
2.Description:* Thread state for a thread which has not yet started.
3. Constant type: Runnable
Declaration: public static final Thread.State RUNNABLE
4.Description:* Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.
5. Constant type: Blocked
Declaration: public static final Thread.State BLOCKED
6.Description:* Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait().
7. Constant type: Waiting
Declaration: public static final Thread.State WAITING
8.Description:* Thread state for a waiting thread. Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
-
- Object.wait with no timeout
- Thread.join with no timeout
- LockSupport.parkA thread in the waiting state is waiting for another thread to perform a particular action.
- Constant type: Timed Waiting
Declaration: public static final Thread.State TIMED_WAITING
3.Description:* Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
- Thread.sleep
- Object.wait with timeout
- Thread.join with timeout
- LockSupport.parkNanos
- LockSupport.parkUntil
- Constant type: Terminated
Declaration: public static final Thread.State TERMINATED
5.Description:* Thread state for a terminated thread. The thread has completed execution.
// Java program to demonstrate thread states
class thread implements Runnable
{
public void run()
{
// moving thread2 to timed waiting state
try
{
Thread.sleep( 1500 );
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println( "State of thread1 while it called join() method on thread2 -" +
Test.thread1.getState());
try
{
Thread.sleep( 200 );
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public class Test implements Runnable
{
public static Thread thread1;
public static Test obj;
public static void main(String[] args)
{
obj = new Test();
thread1 = new Thread(obj);
// thread1 created and is currently in the NEW state.
System.out.println( "State of thread1 after creating it - " + thread1.getState());
thread1.start();
// thread1 moved to Runnable state
System.out.println( "State of thread1 after calling .start() method on it - " +
thread1.getState());
}
public void run()
{
thread myThread = new thread();
Thread thread2 = new Thread(myThread);
// thread1 created and is currently in the NEW state.
System.out.println( "State of thread2 after creating it - " + thread2.getState());
thread2.start();
// thread2 moved to Runnable state
System.out.println( "State of thread2 after calling .start() method on it - " +
thread2.getState());
// moving thread1 to timed waiting state
try
{
//moving thread1 to timed waiting state
Thread.sleep( 200 );
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println( "State of thread2 after calling .sleep() method on it - " +
thread2.getState() );
try
{
// waiting for thread2 to die
thread2.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println( "State of thread2 when it has finished it's execution - " +
thread2.getState());
}
}
Output:
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 - TERMINATED
Explanation: When a new thread is created, the thread is in the NEW state. When .start() method is called on a thread, the thread scheduler moves it to Runnable state. Whenever join() method is called on a thread instance, the current thread executing that statement will wait for this thread to move to Terminated state. So, before the final statement is printed on the console, the program calls join() on thread2 making the thread1 wait while thread2 completes its execution and is moved to Terminated state. thread1 goes to Waiting state because it is waiting for thread2 to complete it’s execution as it has called join on thread2.