Wednesday, December 23, 2015

Life Cycle Of Thread in java With Example

In this post we will know and understand life cycle of Thread in java. This is one of the important concept in Core Java. A Thread can be any one of the five states. The Life Cycle of Thread in java is controlled by JVM. The Thread States are given below:

a) New or born State                                          
b) Ready or Runnable State
c) Running State
d) Blocked State
e) Dead State















a) New State:

    When we write new MyThread() of  the Thread() class  the Thread has born

b) Ready State:  When we call start() method then the thread enter into Runnable State
                                   Ex: t.start();

Note:  After Starting a Thread we can not restart same thread once again, it violation leads to Runtime Error  saying"IlleagalThreadStateException"

Ex:  MyThread t=new MyThread();
           t.start();
           t.start();//IllegalThreadStateException;


Yield():

The thread which is called yield() method temporarily pause the execution to give the chance for remaining threads of same priority. If there is no waiting thread or all waiting threads having low priority. Then the same thread will get the chance immediately for the execution.

public static native void yield();

Ex:
class MyThread extends Thread
{
public void run()
{
for (int i=0; i< 10 ; i++ )
{
System.out.println("Child Thread");
Thread.yield();
}
}
}
class YieldDemo
{
public static void main(String arg[])
{
MyThread t = new MyThread();
t.start();
for(int i =0;i<10;i++)
{
System.out.println("Main Thread");
}
}
}

In this case main thread will get chance more no of times for execution. Because child thread
intentionally calling “yield()” method. As the yield method is native method some Operating system may not provide the support for this.

Sleep():

If a method has to wait some predefined amount of time with out execution then we should go for sleep() method.

public static void sleep(long ms)throws InterruptedException

public static void sleep(long m, int m)throws InterruptedException

Ex:
class MyThread extends Thread
{
public void run()
{
try
{
for (int i = 0;i<10;i++)
{
System.out.println("This is Lazy Method");
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class SleepDemo
{
public static void main(String arg[])throws InterruptedException
{
MyThread t = new MyThread();
t.start();
System.out.println("Main Thread");
}
}

Output:  












Running State:

The Thread is Running state if Thread Scheduler selected it.

Blocked State:

In this state The Thread might be in sleeping state,waiting state,yield state that means Thread is still alive but not eligible to run.

Dead State:

A Thread is in Dead state or terminate when the Thread is successfully completed run() method.

Read also : MultiThreading In Java



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...