Java – Thread Life Cycle
Thread started by JVM at the starting of a program is known as main thread or parent thread.
And the threads created by the programmer are called child threads.
Every thread has some different state from the moment of its creation till its death (termination).
This process of getting different states at different situations is called life cycle of the thread.
The life cycle of any thread in Java is as shown below:
The thread created by a programmer using new operator before invoking the start() method on it is called new/born thread.
Runnable means, capable of running/execution.
After invoking the start() method by a thread and before getting it selected by thread scheduler for execution, the thread is in a runnable state. At this stage, it is ready to get executed as soon as it gets a chance by thread scheduler.
When the thread starts its execution after getting it selected by thread scheduler is said to be in running state.
The blocked state contains sleeping state, waiting state, suspended state, etc.
The thread may enter into any of these states when it is in the blocked state. It depends on the function invoked by a thread or the condition of the program.
Here the working of a thread is stopped temporarily and not permanently.
When work of thread gets over, that is when it completes run() method, the thread enters into dead state. That is JVM terminates the thread.
Here, it may find difficult to understand the complete life cycle of a thread.
But as we will step forward to learn multithreading in detail, it will be understood very easily.