Thread States In Java

As we already know, multithreading is the most important aspect of Java. Every Java program runs in a single thread called, ‘main’ thread. From within this main thread, multiple threads are spawned and each executes concurrently and each thread will have its own stack. A thread will undergo various stages during its life cycle. In this article, we’ll discuss what are all the stages a thread goes through, and which part of JVM is responsible for moving threads from one state to another state.

The thread scheduler is part of JVM whose primary responsibility is to decide which thread should run and also when to move a thread out of the running state etc. There is no guarantee that the Thread Scheduler chooses a particular thread to run, however, we can influence this behavior using methods like sleep(), yield(), join() and setPriority() from Thread class, and also by using methods like wait(), notify() and notifyAll() from Object class.

Thread States

A thread can undergo the following states during its lifecycle.

  • New
  • Runnable
  • Running
  • Waiting, sleeping or blocked
  • Dead

New

A thread is said to be in new state when we created the thread instance, but we have not yet called start() on the thread newly created thread instance. Even though it is a live thread object, it is not a thread of execution. At this state, thread is not active.

Runnable

In the Runnable state a thread is eligible to run, however the Thread Scheduler not selects the thread to start its execution. A thread will get into Runnable state after we call start() method. Also a thread can come back to Runnable state from other states like sleeping, waiting or blocked states. The important point to remember is that, once we call start() method on any thread instance, the thread will move to Runnable state which makes the thread eligible to run. The Thread Scheduler may or may not have picked the thread to run.

Running

Once the Thread Scheduler selects a thread to run from the runnable thread pool, the thread starts execution. This is the state we call a thread is in Running state and thread becomes thread of execution.

Waiting, sleeping or blocked

A thread is not eligible to run if it is in waiting, sleeping or blocked state. But the thread is alive, and it is not in runnable state. A thread can transition back to runnable state when particular event occurs. Thread can move into waiting, sleeping or blocked state for various reasons like waiting for I/O, or call to sleep() method is invoked explicitly to put the thread into sleep etc. Once the I/O is available or sleep period expires the thread can move back to Runnable state. From this point the thread becomes eligible to run again, however Thread Scheduler ultimately decides which runnable thread becomes thread of execution.

Dead

A thread is considered dead once its run() method completed execution. Although a thread’s run() method completed execution it is still a Thread object, but this Thread object is not a thread of execution. Once the thread completes its run() method and dead, it cannot be brought back to thread of execution or even to runnable state. Invoking start() method on a dead thread causes runtime exception.

Thread Priorities

Though we cannot control Thread Scheduler which decides which thread is chosen to run, we can recommend the order in which threads are scheduled to run. Each thread has a priority range between MIN_PRIORITY (constant value of 1), MAX_PRIORITY (a constant value of 10) and NORM_PRIORITY (constant value of 5). Usually threads with higher priority will be allocated processor time to execute before threads with lower priority, but this behavior is not guaranteed. Our program logic should never depend on thread priorities, the reason being we cannot fully control the order of thread execution but we can only influence it by setting priorities.

On most JVM implementations Java threads are mapped to native OS threads. So we should consider the threading capabilities of the OS platform in which our multi-threaded program runs. Controlling thread behavior using priorities may be different from platform to platform. If application portability is important, we should not rely on thread priorities.

Note: JVM (Java Virtual Machine) is the specification that provides a runtime environment to execute the bytecode. JVM supports Java and many other languages known as JVM languages, the program written in these languages is compiled into the bytecode and then executed by the JVM.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

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


Scroll to Top