Explain Life cycle of threads in Java

Life cycle of threads:

During the life time of a thread, there are many states it can enter. They are

1. newborn state

2. runnable state

3. running state

4. blocked state

5. dead state

A thread is always in one of these five states. It can move from one state to another via a variety of ways.

life cycle of threads

Newborn state:

When we create a thread object, the thread is born and is said to be in newborn state, the thread is not yet scheduled for running. At this state we can do only one of the following things with in:

® schedule it for running using star( ) method

® kill it using stop( ) method

If scheduled, it moves to the removable state  if we attempt to use any other method at this stage. An exception will be thrown.

newborn state

Runnable state:

The runnable state means that the thread is ready for execution and is waiting for the availability of the processor. That is the thread has joined the queue of threads that are waiting for execution. If all threads have equal priority then they are given time slots for execution in round robin fashion i.e first come, first serve manner.

 However, if we want a thread to control to another thread to equal priority before its turn comes, we can do so by using the yield( ) method.


runnablestate

Running state:

Running means that the processor has given in time to the thread for an execution. The thread runs until it relinquished control on its own or it is accumulated by a higher priority thread. A running thread may relinquish in control in one of the following situations.

1. It has been suspended using suspend( ) method. A suspended thread can be retrieved by using the resume() method. This approach is useful when we want to suspend a thread for some time due to certain reason, but do not want to kill it.

runningstate2

2. It had been made to sleep. We can put a thread to sleep for a specified time periods using the method sleep(time) where time is in milliseconds. This mean that the thread is out of the queue during this time period. The thread re–enters the runnable state as soon as this time period is elapsed.

runningstate1

3. It has been told to wait until some event occurs. This is done using wait( ) method. The thread can be scheduled to run again using the notify( ) method.

runningstate

Blocked state:

A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently the running state. This happens when the thread is suspended, sleeping , or waiting in order to satisfy contain requirements. A blocked thread is considered not runnable but not dead and therefore fully qualified to an again.

Dead state:

Every thread has a life cycle. A running thread ends in life when it has  completed executing its runs method.  It is a natural death. However, we can kill it by sending the stop message to it at any state thus causing a premature death to it. a thread can be killed as soon it is born, or while it is running, or even when it is in “not runnable” (blocked ) condition.

Ex:

class A extends Thread
{
public void run( )
{
for(int i=1; i<=5;i++)
{
if(i==1) yield();
System.out.println(“\t from thread A:i=”+i);
}
System.out.println(“exit from A”);
}
}

class B extends Thread
{
public void run()
{
for(int j=1; j<=i; i++)    
{
System.out.println(“\t from thread B:j=”+j);
if(j==3) stop();
}
Syetem.out.println(“exit from B”);
}
}

class C extends Thread
{
public void run()
{
for(int k=1; k<=5; k++)
{
System.out.println(“\t from thread C:k=”+k);
if(k==1)
try
{
sleep(1000);
}
catch (Expression e)
}
}
System.out.println(“exit from c”);
}

class Threadmethod
{
public static void main(String args[ ] )
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
System.out.println(“start thread A”);
threadA. start();
System.out.println(“start thread B”);
threadB. start();
System.out.println(“start thread C”);
threadC. start();
System.out.println (“end of main thread”);
}
}

Related

Java 5468561196483858704

Post a Comment

emo-but-icon

item