Thread Life Cycle in Java

Thread Life Cycle in Java tells the various information of thread form born to terminate.

Thread life cycle contains the several stages and at a time any thread can be present  in a single state.

States of Thread life cycle

  1. Born
  2. Ready
  3. Running
  4. Blocked
  5. Sleep
  6. Wait
  7. Dead
Java Thread Life Cycle
Java Thread Life Cycle

Ready:  In this state thread is ready to execute but not running. 

When the start() method calls thread enters from born to ready state.

Running:  When a run() method executes highest priority ready thread assigned a processor and enter in a running state for execution.

Thread can visit more than ones in a running state.

Blocked: When a running thread has a input-output issue it enters in a blocked state from running state and when input-output issue solves it again comes in a running thread.

Sleep : A running thread enter in a sleep state for a specified number of milliseconds when a sleep() method calls.

When sleep time expires then thread moves to ready state.

Wait : When a low priority thread executing and high priority thread comes then high priority thread get preference and enter in a running state. Then low priority thread leaves a running state and moves to wait state for indefinite time.

When a high priority thread execution completes it calls waiting thread for execution by notify () & notifyall () method.

Dead: A running thread is enter in a dead state when its execution completes (run() method completes)   or terminate for any reason.

Multithreading in java example programs

  1. Created a class Thread1 that extends Thread class
  2. main() creates two thread t1 and t2.
  3. To start thread start method is called t1.start() and t2.start()
  4. Start method calls the run method to execute the work
  5. Thread.sleep() send thread to sleep state for 500 milliseconds
  6. From sleep state thread come back to ready then running state
  7. After completing work thread get dead.

Read More

Multithreading in Java