Java. The states of the thread of execution

The states of the thread of execution. The getState() method. Example


Contents


Search other resources:




1. The states of threads of execution and their representation in Java. The Thread.State enumeration

Once created, a thread of execution can be in several states. In Java, the states of threads of execution are defined by predefined constants from the State enumeration of the Thread class. Below is a description of these states

BLOCKED     - The thread has suspended execution because it is waiting to acquire a lock
NEW         - The thread has been created, but it has not yet started its execution
RUNNABLE   - The thread is currently executing or will start executing when
             it gains access to the CPU
TERMINATED - The thread has suspended execution for a specified period of
             time after calling the sleep(), wait(), join() methods
WAITING     - The thread has suspended execution until it waits for some action 
              (calling the wait() or join() methods without a specified timeout)

Figure 1 depicts the possible changes and directions of thread states.

Java. Threads. Thread states diagram

Figure 1. Thread states diagram

 

2. The getState() method. General form

The getState() method of the Thread class is used to get the state of a thread of execution. The general form of the method is as follows:

public Thread.State getState()

here

  • Thread.State is an enumeration type that defines the possible states of the thread of execution: NEW, BLOCKED, RUNNABLE, TERMINATED, WAITING.

If a class implements a thread of execution, then in this class you can get the state of the thread approximately as follows:

class MyThread implements Runnable {
  // Reference to the thread
  Thread thr;

  // Class constructor
  MyThread() {

    ...

    // Create a thread
    thr = new Thread(this);

    ...
  }

  // thread code
  run() {
    // Get the state of thread
    Thread.State ts;
    ts = thr.getState();

    // Handle thread state
    if (ts == Thread.State.BLOCKED) {
      ...
    }
    else
    if (ts == Thread.State.NEW) {
      ...
    }
    else
    if (ts == Thread.State.RUNNABLE) {
      ...
    }
    else 
    if (ts == Thread.State.TIMED_WAITING) {
      ...
    }
    else {
      // Process the state Thread.State.WAITING
      ...
    }
  }
}

 

3. An example that demonstrates the definition of states of threads of execution

The example defines the states of the main and child threads. The state value is displayed in the static State method of the ProcessState class.

// A class containing a static method that handles thread state
class ProcessState {

  public static String State(Thread.State ts) {
    if (ts == Thread.State.BLOCKED)
      return "BLOCKED";
    if (ts == Thread.State.NEW)
      return "NEW";
    if (ts == Thread.State.RUNNABLE)
      return "RUNNABLE";
    if (ts == Thread.State.TIMED_WAITING)
      return "TIMED_WAITING";
    return "WAITING";
  }
}

// The class that encapsulates the thread of execution
class MyThread implements Runnable {
  Thread t;

  // Constructor
  MyThread(String threadName) {
    // Create a thread named threadName and start it for execution
    t = new Thread(this, threadName);

    // The thread has not been started yet, display the state of the thread
    Thread.State ts = t.getState();
    System.out.println("State of MyThread in constructor: " + ProcessState.State(ts));

    // Start the thread for execution
    t.start();
  }

  // Thread execution code
  public void run() {
    Thread.State ts = t.getState();
    System.out.println("State of MyThread in run() method: " + ProcessState.State(ts));
  }
}

public class ThreadState {

  public static void main(String[] args) {
    // 1. Determine the state of a child thread
    MyThread mt = new MyThread("mt"); // Create and start a thread

    try {
      mt.t.join();

      System.out.println("State after join(): " + ProcessState.State(mt.t.getState()));
    } 
    catch (InterruptedException e) {
      e.printStackTrace();
    }

    // 2. Determine the state of the main thread
    Thread thr = Thread.currentThread();
    try {
      Thread.sleep(2000);
      System.out.println("Main thread after sleep(): " + ProcessState.State(thr.getState()));
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

The result of the thread

State of MyThread in constructor: NEW
State of MyThread in run() method: RUNNABLE
State after join(): WAITING
Main thread after sleep(): RUNNABLE

 


Related topics