Thread life cycle and states

Understanding the thread life cycle and states is very important when working with threads and multithreaded environments. In the previous examples, we saw how we can create the Java thread object using the Thread class and the Runnable interface. But to start the thread, we have to first create the thread object and call its start() method to execute the run() method as a thread.

The following are different states of the thread life cycle in Java:

  • New: The thread is in the new state when it is created with a new operator. At this stage, the thread is not alive.
  • Runnable: The thread is in the runnable state when we call the start() method of the thread object. At this stage, the thread scheduler still does not pick it for running.
  • Running: The thread state is changed from runnable to running when the thread scheduler has selected it.
  • Blocked/waiting: The thread state is blocked/waiting when it is currently not eligible to run.
  • Terminated/dead: The thread state is terminated/dead when it executes its run method. At this stage, it's considered to be not alive.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset