Thread Life Cycle Example in java

Thread life cycle example in Java explains the life cycle of a Thread using a simple example.

Thread Life Cycle Example in java

Thread life cycle example in Java explains the life cycle of a Thread using a simple example.

Thread Life Cycle Example in java

Thread Life Cycle Example in java

In this section we will read about the life cycle example of Thread in Java.

Life cycle of Thread explains you the various stages of Thread in Java. These states are New State, Runnable State, Running State, Blocked State and Dead State.

New State : A Thread is called in new state when it is created. To create a new thread you may create an instance of Thread class or you can create a subclass of Thread and then you can create an instance of your class.

Example :

Thread thread = new Thread();

Newly created thread is not in the running state. To move the thread in running state the start() method is used.

Runnable State : A Thread is called in runnable state when it is ready to run and its waiting to give control. To move control to another thread we can use yield() method.

Example :

Thread thread = new Thread();
thread.yield();

Running State : A Thread is called in running state when it is in its execution mode. In this state control of CPU is given to the Thread. To execute a Thread the scheduler select the specified thread from runnable pool.

Blocked State : A Thread is called in blocked state when it is not allowed to enter in runnable and/or running state. A thread is in blocked state when it is suspend, sleep or waiting.

Example :

Thread thread = new Thread();
thread.sleep();

Dead State : A Thread is called in dead state when its run() method execution is completed. The life cycle of Thread is ended after execution of run() method. A Thread is moved into dead state if it returns the run() method.

We will explain the Life cycle of thread mentioned above using a simple example. This section will contain the line by line description of example.

Example

Here we are giving a simple example of Thread life cycle. In this example we will create a Java class where we will create a Thread and then we will use some of its methods that represents its life cycle. In this example we have used the methods and indicate their purposes with the comment line. In this example you will see we have created two Thread subclasses named A.java and B.java. In both of the classes we have override the run() method and executed the statements. Then I have created a Main.java class where I have created instances of both classes and uses the start() method to move the threads into running state, yield() method to move the control to another thread, sleep() method to move into blocked state. And after successfully completion of run() method the Thread is automatically moved into Dead state.

A.java

public class A extends Thread {
public void run()
{
System.out.println("Thread A");
System.out.println("i in Thread A ");
for(int i=1;i<=5;i++)
{
System.out.println("i = " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Thread A Completed."); 
}
}

B.java

public class B extends Thread {

public void run()
{
System.out.println("Thread B");
System.out.println("i in Thread B ");
for(int i=1;i<=5;i++)
{
System.out.println("i = " + i); 
}
System.out.println("Thread B Completed.");
}
}

Main.java

public class Main {
	
	public static void main(String[] args) {
		
		//life cycle of Thread
		// Thread's New State
		A threadA = new A();
		B threadB = new B();
		// Both the above threads are in runnable state
		
		//Running state of thread A & B		
		threadA.start();
		
		//Move control to another thread
		threadA.yield();
		//Bolcked State of thread B
		try {			
			threadA.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		threadB.start();		
		System.out.println("Main Thread End");
	}
}

Output

When you will execute the above Java class (Main.java) you will get the output as follows :

Download Source Code