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.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021