June 17, 2022

Life Cycle of a Thread (Thread States) in Java

In order to understand Java multi-threaded programming better you should have a good idea of Thread life cycle in Java and various thread states in Java.

Once you create a thread in Java a thread can be in one of the following states-

  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • TIMED_WAITING
  • TERMINATED
Thread states in Java

Thread states in Java explained

Various thread states in Java multi-threading are as follows.

  1. New State– A thread in Java is in new state when it is created but not yet started i.e. start() method is not called on the thread object.
  2. Runnable state- A thread transitions to a runnable state when start() method is called on the thread object. A thread in runnable state is scheduled to run by JVM but it may not start running until it gets CPU cycle.

    A Java thread after start running may change to one of these states- waiting, blocked, resumed running and terminated.

  3. Blocked state- A running thread can change state to blocked state and become temporarily inactive when it is waiting for a monitor lock. As example if one thread has entered a synchronized block other threads trying to acquire a lock to enter the same synchronized block will be blocked.
    synchronized (object reference) {   
      //critical section
    }

    Once the thread that has the lock releases it, scheduler will randomly pick one of the thread blocking on that synchronized block and change its state so it can resume running. A thread in blocked state won’t get any CPU time.

  4. Waiting state- A running thread may move to indefinite waiting state by calling either Object.wait() or Thread.join() method with out any time out parameter.

    A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

  5. Timed_Waiting state- A thread is in timed waiting state when it calls one of the following methods with a timed out parameter.

    • Thread.sleep
    • Object.wait with timeout
    • Thread.join with timeout
    • LockSupport.parkNanos
    • LockSupport.parkUntil

    For example-

    MyThread thread = new MyThread();
    thread.start();
    try {
      thread.sleep(500);
    } catch (InterruptedException e){
    
    }

    This code will cause the currently executing thread to sleep (temporarily cease execution) for 500 milliseconds.

  6. Terminated state- A thread that has completed execution goes into terminated state.

Getting Thread state in Java code

You can get thread state in Java by calling getState() method on the thread instance which returns a Thread.State enum.

class AnotherThread implements Runnable{
  @Override
  public void run() {
    System.out.println("run method of Another thread --" 
      + Thread.currentThread().getName());	
  }	
}

public class ThreadDemo {
  public static void main(String[] args) {
    Thread thread = new Thread(new AnotherThread(), "AnotherThread");
    Thread.State ts = thread.getState();
    System.out.println("Thread state-- " + ts.name());
    thread.start();
    System.out.println("Thread state after start-- " + thread.getState().name());
  }
}
Output
Thread state-- NEW
Thread state after start-- RUNNABLE
run method of Another thread --AnotherThread

That's all for the topic Life Cycle of a Thread (Thread States) in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment