Life cycle and states of Thread
Like every object, Java thread also has its own life cycle and it goes into different states in its entire life cycle.
The various states of java thread are as below
1) New
2) Runnable
3) Running (Not a defined state, but we will use it for better understanding)
4) Waiting
5) Timed Waiting
6) Blocked
7) Terminated
New
Whenever we create an instance of Thread, Thread gets a state called “ New ” state.
In this case, Thread is just created but not started, in other words we have a thread object but there is no thread execution.
Below line will create Thread instance and gets it into a “New” status.
- Thread t1 = new Thread ();
Thread t1 = new Thread ();
Runnable
Whenever we start the thread it moves from “ New ” state to “ Runnable ” state.
In this case, thread is ready to be executed but it’s just waiting for the Thread scheduler to pick it for execution.
In this state, a new call stack will be created for the thread.
Below line takes the thread from “ New ” state to “ Runnable ” state.
- t.start();
t.start();
Running
This is not the standard thread state defined by Java but its used to indicate that the thread is currently running or executing the logic.
Moving thread from “ Runnable ” state to “ Running ” state is entirely dependent on Thread scheduler.
Thread scheduler is the one which decides whether to move the thread from “ Runnable ” state to “ Running ” state or put it on hold in “ Runnable ” state by giving chance to other Runnable threads.
Thread scheduler is dependent on operating system.
Most of the operating systems follow Round-robin scheduling.
In this case, each thread is given a fixed amount of processor time called as “ quantum ” or “ time slice ” within which it has to execute.
Any thread which has got this time slice to execute its task is said to be in “ Running ” state.
Once time slice expires, thread will be returned to “ Runnable ” state and another thread will be assigned to the processor.
This process that operating system uses to determine which thread has to go from “ Runnable ” state to “ Running ” state is called “ Thread scheduling ”
This process can be influenced by setting priority to threads.
Note:
Operating system hide the “Running” state from the Java Virtual machine and shows only the “Runnable” state to it and hence there is no standard state defined by Java for “Running” thread.
The process of moving threads from “ Runnable ” state to “ Running ” state and vice versa keeps on happening as per the underlying operating system scheduling mechanism.
Waiting
In this case,Thread will be moved from “ Running ” state to “ Waiting ” state by calling its wait() method.
This can be done whenever we want currently running thread to wait for some other thread to execute and notify back to it to continue the execution.
Once the thread wait is notified , then the waited thread will be moved to “ Runnable ” state.
Timed waiting
In this case, Thread will be moved from “ Running ” state to “ Waiting ” state with specified waiting time.
This can be done whenever we want currently running thread to wait for some specified amount of time.
Once the specified amount of time is completed, then the waited thread will be moved to “ Runnable ” state.
We can put the thread to “ Timed waiting ” by calling sleep(long millis) or wait(long millis) method
If we call sleep(long millis), then thread will be in “ Timed waiting ” state until time expires and then it will be moved to “ Runnable ” state.
If we call wait(long millis) then thread will be in “ Timed waiting ” state until time expires or notified by other thread.
Blocked
In this case, thread will be moved from “ Running ” state to “ Blocked
” state.
This can happen due to various reasons like
Current thread might try to read data from IO stream but there is not data to read.
Thread might be waiting to acquire the lock to enter synchronized block.
Thread will be moved from "Blocked"
state to “ Runnable ” state once the data on the stream is available or lock is released by other thread.
Terminated
Thread will be moved to Terminated state(also called Dead state
) when it completes its execution successfully.
It can also be terminated forcefully by killing it.
loved the simplicity of the explanation on your website