States and Life Cycle of a Java Thread

Dan Dias Abeyesinghe
Javarevisited
Published in
6 min readJan 14, 2024

--

Photo by Kevin Ku on Unsplash

What are Java Threads? A Java thread can be defined as the smallest unit of execution in a Java program, representing an independent path of execution. This allows concurrent execution of many tasks within the same program which results in parallel processing and utilization of system resources. To get a clearer understanding of what Java Threads are and how those are implemented check out my article on Basics of MultiThreading in Java.

Moving on to the life cycle of a Java Thread, a thread could be in many states during its lifetime, it can be divided into 6 states namely,

· NEW

· RUNNABLE

· WAITING

· TIMED_WAITING

· BLOCKED

· TERMINATED

Below diagram is an overview of the life cycle of a Thread at a high level.

We will go through each state and see what each state is about.

NEW STATE

This state is the initial state where a thread could be in, this is the state where a thread is when the Thread object is created but not yet started. In this state, no resources are allocated to it and its run() method is not yet invoked.

As shown in the above code snipped a thread can be in the NEW state when an object is created. When a thread is in this state it is considered to be an empty object and when in this state a thread can only be started by calling the start() method. Keep in mind that calling any other method except for the start() method will not work and will cause an IllegalThreadStateExpression.However, it is important to call the start() method instead of the run() and its importance is further discussed in the Basics of Multithreading article.

RUNNABLE STATE

The transition to this state happens when a newly created thread calls the start() method, when called the start() method the system will first allocate resources to the thread, schedule it and finally it will invoke the run() method.

However, it is important to note that being in the RUNNABLE state doesn’t necessarily mean that a thread is executing. It is totally up to the JVM scheduler which uses the Fixed Priority Pre-emptive Scheduling approach. An example of a thread being in the RUNNABLE state and not running is when a thread is waiting for the processor or other resources from the system. The JVM uses context switching to switch between processes in the Ready and Running states. A clear explanation of it is given below.

Inside the Runnable state

As seen above there are two states inside the RUNNABLE state which are called ready and running respectively, simply when a thread is executing it is in the running state and for any reasons mentioned in the diagram above it has to wait, then it will move to the ready state letting another thread begin its execution, it is important to remember when a thread is pre-empted it does not go to the BLOCKED state but instead it will move the ready state.

BLOCKED STATE

This state is considered a “NON-RUNNABLE STATES” as threads in these states are not available for execution and are usually waiting for some operation completion. states WAITING and TIME_WAITING fall under this category.

When a thread is in the BLOCKED state, it means that the thread is waiting to obtain a monitor lock to enter or re-enter a synchronized block or method. The thread is temporarily unable to run because it is trying to access a resource that is currently held by another thread and will be in the BLOCKED state until it acquires the lock.

As shown above, the increment method is a synchronized method which means at any given time only a single thread can access the method and it is made possible by acquiring the monitor synchronization lock. When multiple threads compete to get the lock, only the thread that acquires it will be able to enter into the body of the synchronized method (critical section) while other threads that are in the RUNNABLE state will move to the BLOCKED state and will remain until the thread with the object lock is done performing and releases the lock.

WAITING STATE

There are various reasons why a thread in the RUNNABLE state goes into the WAITING state. One is when the thread calls the wait() method on itself it will release the lock and go into the WAITING state until notify() or notifyAll() is called.

In the above code snippet the wait() method is called and it will release its lock and move from the RUNNABLE state to the WAITING state until notify() and notifyAll() is called.

The difference between notify() and notfiyAll() is that notify will wake up one random thread only and notifyAll() will wake up all threads in the WAITING state.

Another reason for a thread to go into the WAITING state is the use of the join() method which is used to make a thread wait for another thread to complete its execution.

TIMED_WAITING STATE

A thread that is waiting for a specific time can be found in the TIMED-WAITING state. This state often occurs when a thread invokes methods like sleep(t), wait(t), or join(t) with a specified timeout. During TIMED_WAITING state, the thread is waiting for either the specified time to elapse or until notify(), notify() is called to wake the threads.

Here a thread can be put into sleep for a specific time which makes it release its resources and move from the RUNNABLE to TIMED_WAITING state and wait(t) is similar to this as well.

Here the join() method can also be used with a specific time to indicate that the join would occur after time expiration.

TERMINATED STATE

The final state a thread could be in is this state. This state indicates that a thread has completed its execution. Once a thread finishes running its run() method or if an uncaught exception terminates its execution, the thread enters the TERMINATED state. A thread in the TERMINATED state cannot be restarted.

So in the above, we covered all the states and the life cycle of a Thread from the time it is created until its termination. I hope this article was beneficial and helped you to get a better and clearer understanding of what the states and the life cycle of a Thread are about. Please do feel free to provide feedback on the above and follow for more articles related to this and many more!

--

--