Life Cycle of Thread Simplified in Java




Each Thread is java goes through different states when it is created and when it is dead.Read about Thread creation here.Also read Thread Class here.In this post I will try to cover the different Thread states.
Below are the Thread states:

  • Newborn
  • Runnable
  • Running
  • Blocked
  • Dead
Below are the methods that move Threads from one state to another.
  • start()
  • stop()
  • suspend()
  • resume()
  • sleep()
  • yield()
Out of which sleep(),suspend() and wait() are used to block a Thread.



Here is the pictorial diagram of Thread life cycle:
Newborn: When a Thread is just created by the new statement but not yet run. It is in newborn state.In this case the local data members are allocated and initialized.The newborn Thread is not scheduled for running.At this stage we can do two things:

  1. Schedule it for running using start() method
  2. kill it by using kill() method.
Once scheduled,it moves to the runnable state.Any other attempts to move to other state will be thrown as Exception.


Runnable: The Thread is already ready to run and is awaiting for the control of processor. The Thread may be in queue and waiting their turn to be executed.If all the Threads have equal priority , then they are given time slots for execution in round robin fashion. i.e first come first serve basis.The Thread that relinquishes control joins thh queue at the end and again waits for it's turn.The process of assigning time to Threads is known as time slicing.However if we want a Thread to relinquish control to another Thread equal priority before it's turn come,we need to use yield() method.

Running: The Thread has the control  of the processor. It's code is currently being executed. The thread will continue in this state until it gets preempted by a higher priority Thread or until it relinquishes control.A running Thread may relinquish it's control in the following situation:

  1. It has been suspended using suspend() method.A suspended Thread can be revived by using resume() method.This is useful when we want to suspend a Thread for some reason due to to certain reason,but we do not want to kill it.
  2. If the Thread has been made to sleep using the sleep(timeout) method.This signifies that the Thread is out of the queue for this timeout period.The Thread will reenter into runnable state as soon as the timeout period elapsed.
  3. If the Thread has been told to wait until some event occurs.This is done using wait() method. The Thread can be rescheduled to run again using the notify() method.





Blocked:A Thread is blocked when it is prevented from the Runnable or running state and is waiting for some event in order to reenter the scheduling queue.This happens when the Thread is suspended,sleeping or waiting in order to satisfy certain requirements.A blocked Thread is considered "not runnable" but not dead and therefore fully qualified to run again.

Dead: The Thread has finished it's execution of its run() method or stopped by another Thread.When it completes it execution of run() method,it is said to be natural death.When we kill a Thread by sending stop() method,this is considered as premature death.A Thread can be killed as soon as it is born or while running or in not runnable condition.

Below are the methods to change the Thread status:

start(): A newborn Thread with this method enters into runnable state and java runtime creates a system Thread context and starts its execution.This method for a Thread object can be called once only.

stop(): This method is responsible to stop a Thread immediately.This is often an abrupt way to stop a Thread.This moves the state of the current Thread to Dead state.A Thread will also move to dead state automatically whenit reaches the end of it's method. The stop() method may be used when the premature death of a Thread is required.

suspend(): It temporary stops a thread and later can start the Thread again.

Resume(): This method is received by a suspended Thread.There is no guarantee that the suspended Thread will run immediately as there may be high priority Thread running already or waiting.However this method will make a suspended Thread runnable.

sleep(int n):This method asks java runtime to put current Thread to sleep for n milliseconds.After n milliseconds the Thread will eligible to run again.

Yield(): The yield() method  causes the runtime to switch the context from the current Thread to next available and runnable Thread.This is one way to ensure that the Threads at lower priority do not get started or ignored.

As I said earlier,sleep(),suspend() and wait() are used to block a Thread.A Thread can be temporarily suspended or blocked from entering into runnable and subsequently running state by using either of the following Thread methods:
sleep(int n):  blocked for a specific time n
suspend(): blocked until further order.
wait(): blocked until certain condition occurs
These methods cause the Thread to go into blocked(or not runnable) state. The Thread will return to runnable state when specified time is elapsed in case of sleep(),the resume() method is invoked in case of suspend() and notify() method is called in case of wait().
Life Cycle of Thread Simplified in Java Life Cycle of Thread Simplified in Java Reviewed by Animesh on September 25, 2018 Rating: 5

No comments:

Powered by Blogger.