Interview Questions

What is life cycle of Thread, explain thread states? (

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

22. What is life cycle of Thread, explain thread states? (

Thread states/ Thread life cycle is very basic question, before going deep into concepts we must understand Thread life cycle.

Thread have following states >
New
Runnable
Running
Waiting/blocked/sleeping
Terminated (Dead)


Thread states/ Thread life cycle in diagram :



Thread states in detail :

New : When instance of thread is created using new operator it is in new state, but the start() method has not been invoked on the thread yet, thread is not eligible to run yet.

Runnable : When start() method is called on thread it enters runnable state.

Running : Thread scheduler selects thread to go from runnable to running state. In running state Thread starts executing by entering run() method.

Waiting/blocked/sleeping : In this state a thread is not eligible to run.
Thread is still alive, but currently it’s not eligible to run. In other words.

How can Thread go from running to waiting state?
By calling wait() method thread go from running to waiting state. In waiting state it will wait for other threads to release object monitor/lock.

How can Thread go from running to sleeping state?
By calling sleep() method thread go from running to sleeping state. In sleeping state it will wait for sleep time to get over.

Terminated (Dead) : A thread is considered dead when its run() method completes.

(Continued on next question...)

Other Interview Questions