Java Thread Life Cycle and States

In Java, a Thread is a lightweight process that allows a program to operate more efficiently by running multiple threads in parallel. Internally, JVM creates a Thread and hands it over to the operating system for execution. The operating system then schedules, executes this thread and performs various state transitions between multiple threads.

During state transitions and life cycle, a Thread goes into various states depending on several factors such as thread priority, forcibly suspending a thread or waiting for the output of blocking operations.

1. Thread Life Cycle States

A Java thread can be in any of the following thread states during its life cycle:

  • New
  • Runnable (or Running)
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated

These are also called life cycle events of a thread. Let’s understand each state in more detail.

1.1. New

As soon as, you create new thread, it’s in NEW state. Thread remains in New state until the program starts the thread using its start() method. At this point, the thread is not alive.

Thread thread = new Thread();
System.out.println(thread.getState());   //NEW

1.2. Runnable

Calling the thread.start() method puts the thread in RUNNABLE state. At this point, execution control is passed to OS thread scheduler to finish its execution. The thread scheduler decide from this point that this thread should be executed (also known as dispatching the thread) or should be put on hold to give chance to other runnable threads. Thread scheduling is platform dependent — the behavior of a multi-threaded program could vary across different Java implementations.

In most operating systems, each thread is given a small amount of processor time—called a quantum or timeslice—with which to perform its task. A task utilizing it’s quantum is said to be in RUNNING state. When its quantum expires, the thread returns to the RUNNABLE state, and the operating system assigns another thread to the processor.

The process that an operating system uses to determine which thread to dispatch is called thread scheduling and is dependent on thread priorities.

Thread thread = new Thread();
thread.start();
System.out.println(thread.getState());    //RUNNABLE

The operating system hide the RUNNABLE and RUNNING states from the Java Virtual Machine (JVM), which sees only the RUNNABLE state.

RUNNABLE VS RUNNING

1.3. Blocked

A RUNNABLE thread transitions to the BLOCKED state when it attempts to perform a task that cannot be completed immediately and it must temporarily wait until that task completes.

For example, when a thread issues an input/output request, the operating system blocks the thread from executing until that I/O request completes—at that point, the blocked thread transitions to the RUNNABLE state, so it can resume execution. A blocked thread cannot use a processor, even if one is available.

1.4. Waiting

Usually program put a thread in WAIT state because something else needs to be done prior to what current thread is doing. A thread can be put in waiting state using

  • object.wait()
  • thread.join() or
  • LockSupport.park()

Once the thread wait state is over, its state is changed to RUNNABLE and it is moved back to thread pool.

1.5. Timed Waiting

A RUNNABLE thread can transition to the TIMED WAITING state if it provides an optional wait interval when it’s waiting for another thread to perform a task. You can put a java thread in TIMED WAITING state by calling using following methods:

  • thread.sleep(long millis)
  • wait(int timeout) or wait(int timeout, int nanos)
  • thread.join(long millis)

Such a thread returns to the RUNNABLE state when it is notified by another thread or when the timed interval expires—whichever comes first. Timed waiting threads and waiting threads cannot use a processor, even if one is available.

1.6. Terminated

A thread enters the TERMINATED state (sometimes called the dead state) when it successfully completes its task or otherwise terminated due to any error or even it was forcefully killed.

2. Deadlock and Starvation

Please remember that though JVM and OS thread scheduler do their best yet sometimes threads can cause starvation or deadlock.

A deadlock occurs when a waiting thread (let us call this thread1) cannot proceed because it is waiting (either directly or indirectly) for another thread (let us call this thread2) to proceed. And simultaneously thread2 cannot proceed because it is waiting (either directly or indirectly) for thread1 to proceed.

3. Conclusion

In this Java concurrency tutorial, we learned about the lifecycle of a Thread in Java, and how the state transitions happen between different states of the Thread.

Happy Learning !!

Comments

Subscribe
Notify of
guest
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode