Java Examples - Get Thread State

Java 实例Java Examples

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

//Java Program - Demonstrating Thread State class thread implements Runnable { public void run() { //thread2 - Timed Waiting 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); //Created thread1, now it is in the initial state System.out.println("State of thread1 after creating it - " + thread1.getState()); thread1.start(); //thread1 - Ready 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); //Created thread1, now it is in the initial state System.out.println("State of thread2 after creating it - "+ thread2.getState()); thread2.start(); //thread2 - Ready State System.out.println("State of thread2 after calling .start() method on it - " + thread2.getState()); // moving thread1 to timed waiting state try { //moving - Timed Waiting 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 terminate thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("State of thread2 when it has finished it's execution - " + thread2.getState()); } }

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 - TERMINATED

Java 实例Java Examples

Other Extensions