Java Thread Lifecycle

Srini
5 min readAug 28, 2021

What is a Thread?

A Thread is an independent path of execution in a program. Many threads can be spawned within a program, and these threads can run concurrently.

  • A thread is a thread of execution.
  • The JVM allows an application to have multiple threads of execution running concurrently.
  • Each thread has a priority. Threads with higher priority are executed in preference to the threads with the lower priority.
  • Each thread may or may not have also marked a Daemon.
  • When a JVM starts up, there is usually a single non-daemon thread (which typically invokes the main method).
  • The JVM continues to execute the threads until either of the following occurs:
  • The exit method of Runtime class has been invoked and the security manager has allowed the exit method to execute.
  • All threads that are not daemon threads have died, either by returning from the run method call or by throwing an exception.

How does the JVM keep track of each thread’s execution?

The JVM gives each thread its own method-call stack. In addition to keep tracking of the current byte code instruction, the method-call stack also tracks local variables, parameters the JVM passes to a method, and the method’s return value.

Creating and Running Threads:

Threads can be created in two ways:

  • Extending Thread class and overriding run() method.
  • Building a class that implements Runnable interface and then creating an object of Thread class passing the Runnable object as a parameter.

Below is a simple example of creating threads using the second approach. This example creates 10 threads, each of the thread calculate and prints the multiplication tables from 1 to 10.

Steps to implement the example:

  • Create a class Name Calculator implementing Runnable interface.
public class Calculator implements Runnable
  • Declare a private int attribute named number and implement the constructor to initialize its value.
private int number;
  • Implement the run() method. This method calculates the multiplication table of the number.
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.printf("%s: %d * %d = %d\n", Thread.currentThread()
.getName(), number, i, i * number);
}
}
  • Implement the main class of the application, which contains main() method.
  • Inside the main() method, create a for loop with 10 iterations. Inside for loop, create an object of Calculator class, an object of the Thread class, pass the object of Calculator class as a parameter, and invoke the start() method of the Thread object.
public class Main {
public static void main(String[] args) {
for(int i=0; i
Below is the partial output of a sample run of the above code:
  • Every Java Program has at least one thread in Execution. When we run a program, JVM runs the execution thread that calls main() method.
  • Creating an object to the Thread class doesn’t create an execution of the Thread. Or calling the run() method of the implemented Runnable interface will not create a new execution of Thread. Invoking start() method on creates a new execution thread.
  • When we call the start() method of Thread object, it creates another execution thread. Our program will have as many threads as calls to the start() method are made.
  • A Java program ends when all its threads(all non-daemon) finishes.
  • If the initial thread(the one that executes main() method) ends, the rest of threads continue with their execution until they finish.
  • If a thread is exited by calling System.exit(), all threads end their execution.

Life cycle of a Thread

Understanding the lifecycle of threads is very important while programming in Threads.

A Thread can be various states during its life time. The following diagram describes all the states a thread can undergo and the method calls that cause the transition from one state to another:

Java Thread States

  1. Runnable: A Thread starts its life from this state. Thread first enters into this state after the start() method of the thread is invoked. In this state, thread will be waiting for the scheduler to pick the thread for execution. The scheduler picks the threads based on their priorities. A thread can also re-enter this state either after running, waiting, sleeping or also while coming back from blocked state.
  2. Running: A thread is in running state means its currently running. There are several ways for thread to go into Runnable state. But there is only one way to come into Running state: the scheduler selects the thread for execution for the runnable pool. A thread runs until its swapped out, becomes blocked, or voluntarily give up its turn by invoking the static method Thread.yield().
  3. Dead: A thread enters this state when it completes execution. It may also enters this state when it is terminated by unrecoverable error condition. If a thread goes to this state means it can not be run again.
  4. Sleep: A Java thread may be forced to sleep (suspend) for some predefined time. In this state, thread is still alive but is not runnable, it might be return to runnable state later. It can throw InterruptedException.
  5. Blocked: A thread can enter blocked state because of waiting for the resources that are held by another thread.
  6. Blocked on I/O: Thread enters this state because of waiting for I/O resources. In this case, the thread will be sent back to Runnable state after the availability of resources.
  7. Blocked on Synchronization: Thread may enters this state while waiting for object lock. Thread will be moved to Runnable state after it acquires lock.
  8. Waiting state: A call to Object.wait() method causes the current object to wait. The thread remains in waiting state until some other thread invokes notify() or notifyAll() method of this object.

Release 5.0 introduced the Thread.getState() method, which results in one of the following Thread.State values:

  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • TIMED_WAITING
  • TERMINATED

The Thread class API also introduced a method isAlive(), which returns true if the thread has already been started and not stopped. If the method isAlive() returns false, the thread is either New, or is Dead.

--

--