Life cycle of a thread in Java

Life cycle of a thread in Java:

Understanding Thread Life Cycle and Thread States are very important when you are implementing Threads and multi-threaded programming for your application.

Thread States

Below diagram shows different states of a thread in java, remember that we can create a thread and start it but how the thread states change from Runnable to Running and Blocked depends on the OS’s thread scheduler and java doesn’t have much control.


1) New Born state

Thread t1 = new Thread();

In new born state, the thread object is just created but not started yet, we can say it is inactive. In the above statement t1 thread is created but not started. To make the thread active, call start() method on the thread object as t1.start(). This makes the thread active and now eligible to run, now it goes to runnable state.

2) Runnable state

When the thread is active, the first and foremost job, it does implicitly, is calling run() method. When the thread is executing the run() method, we say, the thread is in runnable state. As it is a callback method, all the code expected to run by the thread, should be written here. In this state, the thread is active.

3) Running

When thread is executing, means run() method has already been called, it’s state is changed from runnable to Running. Thread scheduler picks one of the thread from the runnable thread pool and change it’s state to Running and CPU starts executing this thread’s run() method. A thread can change state to Runnable, Dead or Blocked from running state depends on time slicing, thread completion of run() method or waiting for some resources.

4) Blocked state

Programmer can make a running thread to become inactive temporarily for some period. In this period (when inactive after starting), the thread is said to be in blocked state. The blocked state thread, as inactive, is not eligible to processor time. This thread can be brought back to the runnable state at any time. A thread can go a number of times from runnable state to blocked state and vice versa in its life cycle.

5) Dead state

When the execution of run() method is over, it comes to dead state. It is done implicitly by JVM. In dead state, the thread object is may be garbage collected depends upon the garbage collector. It is the end of the life cycle of thread. Once a thread is in dead state, it cannot be restarted again.

A thread can be killed and brought to dead state, anytime from any state, by calling explicitly stop() method.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *