JAVA MULTITHREADING — Thread State and LifeCycle

Thread State and LifeCycle in Java Multithreading.

Know how a thread transitions from one state to another.

Vikram Gupta
Javarevisited
Published in
5 min readFeb 18, 2023

--

Thread state and life-cycle

In Java programming, a thread state represents the current state of a thread in a multithreading environment. The thread state is an important concept in multithreading as it determines the behavior of a thread at any given time.

Thread States:

A Thread Can Be In One Of The Following States At A Given Time In Its Lifecycle:

Thread state and life-cycle

NEW: Thread state for a thread that has yet to start. When we create a thread it goes to the NEW state.

Sample Code Example:

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("The Thread state is : "+ Thread.currentThread().getState());
}
});

System.out.println("The Thread state is : "+ thread.getState());

The above code just prints NEW and the run method doesn’t get executed because the thread has yet not started.

RUNNABLE: Thread state for a runnable thread. A thread in the runnable
state is executing in the Java virtual machine but it may
be waiting for other resources from the operating system
such as a processor.

Sample Code Example:

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("The Thread state is : "+ Thread.currentThread().getState());
}
});

System.out.println("The Thread state is : "+ thread.getState());
thread.start();

The output of the above code :

The Thread state is: NEW
The Thread state is: RUNNABLE

When the thread gets started it transitions from NEW state to RUNNABLE state.

BLOCKED: Thread state for a thread blocked/waiting for a monitor lock or intrinsic lock of a shared object or resource.
A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait() .

Sample Code Example:

//Method used by reader thread
public synchronized String read(){
while (this.isEmpty);// wait till a message is written by Writer Thread
this.isEmpty = true;//make empty flag true, as reader thread will read the msg
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
return this.msg ;
}

When two reader threads try to execute the above read() method but share a common object that will call the read() method, then because of the synchronization one thread acquires the lock on the object and goes to a RUNNABLE state and the other thread goes to BLOCKED state as it is waiting for the lock to be released by the first thread.

For a complete example, please refer to this blog post —

WAITING: Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods: Object.wait() , Thread.join() and LockSupport.park() with no timeout. 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 same object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

Sample Code Example:

//Method used by reader thread
public synchronized String read() {
while (empty) {
try {
/*
Reader thread releases ownership of lock and waits
until Writer thread notifies Reader thread waiting on
this object's lock to wake up either through a call to
the notify() method or the notifyAll() method.
*/
wait();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "Interrupted.");
}
}
empty = true;//Reader reads the message and marks empty as true.
/*
Wakes up all threads that are waiting on 'message' object's monitor(lock).
This thread(Reader) releases the lock for 'message' object after completing
read() method.
*/
notifyAll();
return message;//Reader reads the message.
}

The reader thread will call the wait() method on an object(basically a message object) when there isn’t a message available and hence it will transition from RUNNABLE to WAITING state.

For a complete example, please refer to this blog post —

TIMED_WAITING: Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
Thread.sleep(),Object.wait(),Thread.join(),LockSupport.parkNanos(), LockSupport.parkUntil() .

Sample Code Example:

//Method used by reader thread
public synchronized String read(){
while (this.isEmpty);// wait till a message is written by Writer Thread
this.isEmpty = true;
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
return this.msg ;
}

When a thread acquires a lock on an object and executes the read() method, and if the isEmpty flag is false, then the thread goes to sleep for 1 second, and its state transitions from RUNNABLE to TIMED_WAITING and wakes up after that and again transitions to RUNNABLE. But the thread doesn’t release the lock in the meantime.

TERMINATED: Thread state for a terminated thread. The thread has completed execution. The Thread transitions to this state after completely executing the run method.

Thread states in Java are managed by the JVM (Java Virtual Machine) and can be monitored and controlled using various APIs provided by the Java language, such as the Thread class and the Thread.State enumeration. Understanding thread states is important in multithreading to ensure that threads are synchronized and are executing in the expected order.

--

--

Vikram Gupta
Javarevisited

Helping Developers to Succeed in Their Technical Interviews.