Wednesday, 8 February 2017

LIFE CYCLE OF THREADS IN JAVA

Understanding the life cycle of thread is very valuable. While a thread is alive, it is in one of the several states. By invoking start() method, it doesn’t mean that the thread has access to CPU and start executing straight away. Several factors determine how to will proceed. 



1. New State – After the creations of Thread instance, the thread is in this state but before the start() method invocation. At this point, the thread is considered as not alive.

2. Runnable (Ready-to-run) state – A thread starts its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return this state after either running, waiting, sleeping or coming back from the blocked state too. On this state a thread is waiting for a turn on the processor.

3. Running state – A thread is in running state which means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state. The scheduler selects a thread from runnable pool.

4. Dead state – A thread can be considered as dead, when its run() method completes. If any thread comes in this state, which means it cannot ever run again.

5. Blocked – A thread can enter in this state, because of waiting for the resources that are held by another thread. 

No comments:

Post a Comment