0% found this document useful (0 votes)
354 views6 pages

Life Cycle of Thread

The document explains the thread life cycle in Java, detailing six states: New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated. It emphasizes the importance of understanding these states for developing efficient multithreaded applications using the java.lang.Thread class. The document provides code examples to illustrate each state and concludes with the significance of multithreading in various applications.

Uploaded by

Vijaya kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
354 views6 pages

Life Cycle of Thread

The document explains the thread life cycle in Java, detailing six states: New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated. It emphasizes the importance of understanding these states for developing efficient multithreaded applications using the java.lang.Thread class. The document provides code examples to illustrate each state and concludes with the significance of multithreading in various applications.

Uploaded by

Vijaya kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

The thread life cycle in Java is an important concept in multithreaded

applications. Although Sun (now Oracle) defines the thread life cycle with only
four states, it's useful to consider six states to clarify the connections and
get better understanding of them. These six states are: New, Runnable, Blocked,
Waiting, Timed Waiting, and Terminated. The JVM manages a thread's life cycle
when written in Java, and understanding these states can help develop more
efficient and reliable multithreaded applications.

Introduction
Before we discuss thread life cycle in Java, let’s understand multithreading.

Multithreading extends the idea of performing multiple tasks at the same time,
which is applicable in real life as well. For example, in a railway ticket
reservation system, multiple customers can access the server at the same time to
book their tickets.

A Java thread goes through several stages in its life cycle, including being
created, started, running, and eventually dying. To better understand these
states during thread execution, we'll use an illustrated graphic and helpful
code examples.

Java Thread States: Thread Life Cycle in Java


Multithreading in Java is implemented using the java.lang.Thread class, which
contains a static enumeration called State that defines the potential states of
a thread. These states represent the phases through which a thread passes during
its execution. At any given moment, a thread can be in one of the following
states:

New
Runnable
Blocked
Waiting
Timed waiting
Terminated
The diagram shown below depict various states of a thread in Java at any instant
of time:

life cycle of thread in java

Let’s now understand each thread state in detail:

1. New
A thread goes through this state when it is newly created by instantiating the
Thread class but the start() method hasn’t been invoked yet. At this state, the
thread is not considered alive as its execution has not started yet.

The following code snippet demonstrates a thread in its NEW state:

public class ThreadState {


public void createNewThread() {
// Create a new thread
Thread thread = new Thread(() -> {});
// Print the thread's state (should be "NEW" at this point)
System.out.println("Thread state: " + thread.getState());
}
}

// Create a new instance of ThreadState


ThreadState threadState = new ThreadState();
// Call the createNewThread() method to create a new thread and print its state
threadState.createNewThread();

In the above example, we’ve created a new thread by calling the Thread
constructor by passing a lambda expression. This lambda expression instantiates
the functional interface Runnable with an empty body of the run() method.

Here’s the output of the above code snippet:

Thread state: NEW

2. Runnable
A java thread goes through this state after the invocation of the start()
method. At this state, a thread is considered alive because it is either running
or ready for execution, but waiting for resource allocation.

In a multi-threaded environment, Thread Scheduler allocates a time slot for each


thread. So, each thread runs for a particular amount of time and then
relinquishes the resource to other threads in the runnable state.

public class ThreadState {


public void createRunnableThread() {
// Create a new thread
Thread thread = new Thread(() -> {});
// Start the thread (this will transition it to the "RUNNABLE" state)
thread.start();
// Print the thread's state (should be "RUNNABLE" at this point)
System.out.println("Thread state: " + thread.getState());
}
}

// Create a new instance of ThreadState


ThreadState threadState = new ThreadState();
// Call the createRunnableThread() method to create a new thread, start it, and
print its state
threadState.createRunnableThread();

In the above example, we’ve created a new thread and called its start() method.

Here’s the output of the above code snippet:

Thread state: RUNNABLE

This output is uncertain because it is possible that the thread in java is


immediately scheduled by the Thread Scheduler and may finish execution before
the control reaches the thread.getState().

3. Blocked or Non-runnable
A thread goes through this state when it is temporarily inactive and not
eligible to run. It enters this state when it is waiting to acquire a monitor
lock and is trying to access a synchronized block or method, which means only
one thread can access the particular resource or method at a time. We can
synchronize a specific block of code in a method or a whole method.

public class ThreadState {


public void createBlockedThread() throws InterruptedException {
// Create two new threads, each running the Task object
Thread thread1 = new Thread(new Task());
Thread thread2 = new Thread(new Task());

// Start the first thread


thread1.start();
// Sleep for one second to give thread1 a chance to acquire the lock
Thread.sleep(1000);

// Start the second thread


thread2.start();
// Sleep for one second to give thread2 a chance to block
Thread.sleep(1000);
// Print the state of thread2 (should be "BLOCKED" at this point)
System.out.println("Thread2 state: " + thread2.getState());

// Exit the program


System.exit(0);
}
}

class Task implements Runnable {


@Override
public void run() {
performTask();
}

public static synchronized void performTask() {


while(true) {
// Thread1 will run forever, holding the lock, and preventing
thread2 from executing
}
}
}

// Create a new instance of ThreadState


ThreadState threadState = new ThreadState();
// Call the createBlockedThread() method to create two threads, start them, and
print the state of one of them
threadState.createBlockedThread();

In the above example, we’ve created two different threads. At first, thread1
starts and executes its run() method to acquire a lock on the synchronized
method performTask(). This method has an infinite loop so thread1 will keep
running forever. After some time, thread2 starts and executes its run() method
and will end up in the blocked state because it cannot acquire a lock on the
performTask() method.

Here’s the output of the above code snippet:

Thread2 state: BLOCKED

4. Waiting
A java thread goes through this state when it is waiting for another thread to
perform a particular activity prior to what the current thread is doing. It
enters this state when its wait() method is invoked. When this prior action is
completed, the scheduler is notified to change the state of the thread to
runnable and move it back to the thread pool.

public class ThreadState


{
public void createWaitingThread()
{
// Create a new thread and start it
Thread thread1 = new Thread(new Thread1Task());
thread1.start();

// Exit the program


System.exit(0);
}
}

class Thread1Task implements Runnable


{
@Override
public void run()
{
// Create a new thread and start it
Thread thread2 = new Thread(new Thread2Task());
thread2.start();

// Wait for thread2 to complete before continuing


try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Thread1 is in waiting state until thread2 finishes execution
}
}

class Thread2Task implements Runnable


{
@Override
public void run()
{
try {
// Sleep for 2 seconds to simulate some work being done
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Thread2 finishes execution and thread1 continues execution
System.out.println("Thread1 State: " + thread1.getState());
}
}

ThreadState threadState = new ThreadState();


threadState.createWaitingThread();

In the above example, thread1 is created and started and its run() method is
executed. In the run() method of thread1, thread2 is created and started and its
run() method is executed. While the processing of thread2 continues, the join()
method on thread2 is invoked that makes thread1 wait until thread2 completes its
execution.

Here’s the output of the above code snippet:

Thread1 state: WAITING

5. Timed Waiting
A thread in java goes through this state when it is waiting for another thread
to perform some action for a specific period. A thread lies in this state until
the timed interval expires or until it’s notified by another thread and then it
returns to the runnable state. A thread enters this state when its sleep(long
millis) or wait(long millis) method is invoked.

public class ThreadState


{
public void createTimedWaitingThread()
{
Thread thread = new Thread(() ->
{
// thread enters sleep state with a timeout period of 5 seconds
Thread.sleep(5000);
});
// TIMED_WAITING
System.out.println("Thread state: " + thread.getState());
}
}

// create instance of ThreadState and call createTimedWaitingThread method


ThreadState threadState = new ThreadState();
threadState.createTimedWaitingThread();

In the above example, a thread is created and started. When its run() method is
executed, it enters sleep mode with a timeout period of 5 seconds. During this
time, the thread remains in the timed-waiting state until the timed interval is
expired, and then it returns to the runnable state.

Here’s the output of the above code snippet:

Thread state: TIMED_WAITING

6. Terminated
A thread in java goes through this last state of its lifetime when it is
successfully executed or was abnormally terminated and no longer consuming any
cycles of CPU. It enters this state when its run() method is entirely executed
when it exits the run() method, or when its stop() method is invoked. In this
situation, a thread is considered dead, and thus if the start() method is
invoked on the dead thread, it’ll raise an IllegalThreadStateException.

public class ThreadState


{
public void createTerminatedThread()
{
Thread thread = new Thread(() -> {});
thread.start();
Thread.sleep(2000); // Sleep for 2 seconds to allow thread to finish
execution
// TERMINATED
System.out.println("Thread state: " + thread.getState());
}
}

ThreadState threadState = new ThreadState();


threadState.createTerminatedThread();

In the above example, we’ve created and started a thread. Then we blocked the
main thread for 2 seconds using the sleep() method. Since our thread is an empty
thread, this will give enough time to complete its execution and enter into the
terminated state.

Here’s the output of the above code snippet:

Thread state: TERMINATED

Conclusion
Multithreading allows performing multiple tasks simultaneously, and a Java
thread goes through several stages in its life cycle, including being created,
started, running, and eventually dying.

The java.lang.Thread class contains a static enumeration called State that


defines the potential states of a thread, and at any given moment, a thread can
be in one of the following states: New, Runnable, Blocked, Waiting, Timed
waiting, or Terminated.

The JVM manages a thread's life cycle when written in Java, and understanding
these states can help develop more efficient and reliable multithreaded
applications. The six states of the thread life cycle in Java are: New,
Runnable, Blocked, Waiting, Timed Waiting, and Terminated.

New state: Occurs when a thread is created but not yet started.

Runnable state: Occurs after the start() method is invoked, and the thread is
alive and either running or waiting for resource allocation.

Blocked state: Occurs when a thread is temporarily inactive and waiting to


acquire a monitor lock to access a synchronized block or method.

Waiting state: Occurs when a thread is waiting for another thread to perform a
specific action.

Terminated state: Occurs when a thread has finished execution.

Multithreading is a powerful tool in Java and understanding its meaning and


usage helps us in optimizing and controlling our multithreaded programs.

There are many useful applications of multithreading, for example, spelling


checker in a word document, multithreaded web servers serving multiple clients
at the same time, gaming applications doing several tasks like updating score,
playing sound, updating GUI, etc.

You might also like