Monday, 21 November 2016

Java Thread Life Cycle

Previously we have discussed about
During the life cycle of thread ,there are many states it can enter. A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.
It can move from one state to another via variety of ways as shown in figure :

Thread State
Description
NEW
A thread that has not yet started is in this state.
RUNNABLE
A thread executing in the Java virtual machine is in this state.
(Non-Runnable) BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.
WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED
A thread that has exited is in this state.

Thread Life Cycle 

NEW state:
public static final Thread.State NEW
When we create a thread object, the thread is born and it 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 it :
1) Scheduled for running using start() method
2) kill by using stop() method
If scheduled it moves to the runnable state.
A NEW born state

RUNNABLE state:
public static final Thread.State RUNNABLE
The runnable state means that the thread is ready for execution and it is waiting for the availability of the procesor.
Relinquishing control using yield() method


BLOCKED (Non-Runnable) state:
public static final Thread.State BLOCKED
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 certain requirements.A blocked thread is not dead but it is fully qualified to run again.

WAITING state:
public static final Thread.State WAITING
A thread is in the waiting state due to calling one of the following methods:
Object.wait with no timeout
Thread.join with no timeout
LockSupport.park

TIMED_WAITING state:
public static final Thread.State TIMED_WAITING
A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:i.e Thread.sleep, Object.wait with timeout and Thread.join with timeout.

TERMINATED state:
public static final Thread.State TERMINATED
The thread has completed execution, or dead state when its run() method exits.



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment