JAVA | Explain thread life cycle with suitable diagram.

Thread Life Cycle Thread has five different states throughout its life. 
1. Newborn State
2. Runnable State
3. Running State
4. Blocked State
5. 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. 

 Newborn state: When a thread object is created it is said to be in a new born state. When the thread is in a new born state it is not scheduled running from this state it can be scheduled for running by start() or killed by stop(). If put in a queue it moves to runnable state. 

Runnable State: It means that thread is ready for execution and is waiting for the availability of the processor i.e. the thread has joined the queue and is waiting for execution. If all threads have equal priority then they are given time slots for execution in round robin fashion. The thread that relinquishes control joins the queue at the end and again waits for its turn. A thread can relinquish the control to another before its turn comes by yield(). 

 Running State: It means that the processor has given its time to the thread for execution. The thread runs until it relinquishes control on its own or it is preempted by a higher priority thread.  

 Blocked state: A thread can be temporarily suspended or blocked from entering into the runnable and running state by using either of the following thread method. 

 suspend(): Thread can be suspended by this method. It can be rescheduled by resume(). 

 wait(): If a thread requires to wait until some event occurs, it can be done using wait method and can be scheduled to run again by notify(). 

 sleep(): We can put a thread to sleep for a specified time period using sleep(time) where time is in ms. It reenters the runnable state as soon as period has elapsed /over 

 Dead State: Whenever we want to stop a thread form running furtherwe can call its stop(). 

The statement causes the thread to move to a dead state. A thread will also move to dead state automatically when it reaches to end of the method. The stop method may be used when the premature death is required. 

Post a Comment

0 Comments