Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Life Cycle Of Thread

Thread has many different state throughout its life such as:

  • • Newborn State
  • • Runnable State
  • • Running State
  • • Blocked State
  • • Dead State

Thread should be in any one state of above and it can be move from one state to another by different methods and ways.

  • When thread instantiation is created then thread will be in new / born state. for exa,
  • • when start() is invoked by MyThread object t, i.e. t.start(); then the thread will enter into ready/runnable state.
  • • then thread scheduler allocates the process then thread will go to the running state. if run() method complets then it will go to dead state.
  • • A running thread calls yield() method that is Thread.yield(); then thread will come to the ready state.
  • • When a running thread calls join() method then the thread enters to the waiting state(Blocked for joining). a thread can call join() method as:
w3-example

t2.join();
t2.join(1000);
t2.join(1000,10);

here t2 is the thread object.then if t2 competes or if time expires or if waiting thread got interrupted, the thread will come out from the waiting state to ready state.

  • if running thread suddenly calls the sleep() then immediately it will enter into sleeping state. it is called as:

    w3-example

    Thread.sleep(1000);
    Thread.sleep(1000,10);

    the thread will come out from sleeping state to ready state if time expires or if sleeping thread got interrupted.
  • • if a thread suddenly calls wait() method, then it will enter into waiting state. we can call wait() as obj.wait(); obj.wait(1000); obj.wait(1000,10); if waiting state got notification then another waiting state to get locked. so when the thread will come out from waiting state to another waiting state, if waiting thread got notification or if time expires or if waiting thread got interrupted. then the thread will come out from another waiting state to ready state if waiting thread got locked.
  • • When a running thread suddenly calls suspend() method then it will enter suspended state by t.suspend(), then thread will come from suspended state to ready state when we call resume() method that is t.resume();
  • • When a running thread suddenly calls stop() method that is t.stop(); then thread will enter to the dead state.

let's see a program using some thread methods such as yield(), sleep() method.


w3-example
class X extends Thread
{
	public void run( )
	{
		for(int i=1;i<=4;i++)
		{
			if(i==2)
			yield( );
			System.out.println("in thread X : i="+i);
		}
	}
}
class Y extends Thread
{
	public void run( )
	{
		for(int j=1;j<=4;j++)
		{
			System.out.println("in thread Y : j="+j);
		}
	}
}
class Z extends Thread
{
	public void run( )
	{
		for(int k=1;k<=4;k++)
		{
			System.out.println("in thread Z : k="+k);
			if(k==1)
			try
			{
				sleep(1000);
			}
			catch(Exception e)
			{
			}
		}
	}
}
class TestMethods
{
	public static void main(String[] args)
	{
		X ob1=new X( );
		Y ob2=new Y( );
		Z ob3=new Z( );
		ob1.start( );
		ob2.start( );
		ob3.start( );
	}
}
Output

in thread Y : j=1
in thread Z : k=1
in thread X : i=1
in thread Y : j=2
in thread X : i=2
in thread Y : j=3
in thread X : i=3
in thread Y : j=4
in thread X : i=4
in thread Z : k=2
in thread Z : k=3
in thread Z : k=4

It is pointed that sleep( ) method throws an exception. In this example, sleep( ) method is enclosed with in a try block and followed by a catch block.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext