Thread Life Cycle Example in java

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


 

Ads

Tutorials   
Threading In Java Example Thread Life Cycle Example in java What Is Thread In Java? Creating multiple Threads Daemon Threads Synchronization in java with example Thread priority in java Threads in Java Java Multithreading Example Thread Synchronization in Java Overview of Networking through JAVA,How to retrieve URL information URL in term of Java Network Programming Overview of Networking through JAVA,Convert URI to URL Socket and ports Server Sockets Overview of Networking through JAVA,Find the Host name in reverse of given IP address Print the URL of a URLConnection Networking in Java Computer Networking Overview of Networking through JAVA,Getting the Local port Overview of Networking through JAVA Overview of Networking through JAVA Overview of Networking through JAVA,Getting list of Local Interfaces on a machine Overview of Networking through JAVA Overview of Networking through JAVA,Find Your Host Name Overview of Networking through JAVA,To retrieve the IP address from Host Name, vice-versa Open a URLConnection to specific website address Return the MIME Header Construct a DatagramSocket on an unspecified port Datagram in network environment Construct a DatagramPacket to receive data Client-Server Architecture Thread Priorities Synchronized Threads Multithreading in Java Interthread Communication Creation of MultiThreads Threading in Java Thread Priorities Inter-Thread Communication Synchronized Threads Creation of Multiple Threads Threading in Java Daemon Threads Deadlock Synchronization Life Cycle of Threads Thread Constructors Thread Creation Overview of Thread
Ads

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

Advertisements

Share on Google+Share on Google+

Thread Life Cycle Example in java

Posted on: July 25, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Thread Life Cycle Example in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads