- New (created but start mtd not called)
- Runnable (Ready to run, waiting for CPU cycles)
- Running (allocated CPU cycle)
- Suspended (timed sleeping)
- Wait (waiting for a notify)
- Stopped (Completed)
There are five states New,Runnable,Running,Blocked,Dead
New state – After the creations of Thread instance the
thread is in this state but before the start() method
invocation. At this point, the thread is considered not
Runnable (Ready-to-run) state – A thread start its life
from Runnable state. A thread first enters runnable state
after the invoking of start() method but a thread can
return to this state after either running, waiting,
sleeping or coming back from blocked state also. On this
state a thread is waiting for a turn on the processor.
Running state – A thread is in running state that means the
thread is currently executing. There are several ways to
enter in Runnable state but there is only one way to enter
in Running state: the scheduler select a thread from
Dead state – A thread can be considered dead when its run()
method completes. If any thread comes on this state that
means it cannot ever run again.
Blocked - A thread can enter in this state because of
waiting the resources that are hold by another thread.
Ready-to-run: A thread starts its life cycle with a call to start().
MyThread aThread = new MyThread();
A call to start() will not immediately start thread's execution but rather will move it to pool of threads waiting for their turn to be picked for execution. The thread scheduler picks one of the ready-to-run threads based on thread priorities.
Running : The thread code is being actively executed by the processor. It runs until it is swapped out, becomes blocked, or voluntarily give up its turn with this static method
Please note that yield() is a static method. Even if it is called on any thread object, it causes the currently executing thread to give up the CPU.
Waiting : A call to java.lang.Object's wait() method causes the current thread object to wait. The thread remains in "Waiting" state until some another thread invokes notify() or the notifyAll() method of this object. The current thread must own this object's monitor for calling the wait().
Sleeping : Java thread may be forced to sleep (suspended) for some predefined time.
Please note that static method sleep() only guarantees that the thread will sleep for predefined time and be running some time after the predefined time has been elapsed.
For example, a call to sleep(60) will cause the currently executing thread to sleep for 60 milliseconds. This thread will be in ready-to-run state after that. It will be in "Running" state only when the scheduler will pick it for execution. Thus we can only say that the thread will run some time after 60 milliseconds.
Blocked on I/O. : A java thread may enter this state while waiting for data from the IO device. The thread will move to Ready-to-Run after I/O condition changes (such as reading a byte of data).
Blocked on Synchronization. : A java thread may enter this state while waiting for object lock. The thread will move to Ready-to-Run when a lock is acquired.
Dead : A java thread may enter this state when it is finished working. It may also enter this state if the thread is terminated by an unrecoverable error condition.
When we create a thread it will be in Newborn State.
The thread is just created still its not running.
We can move it to running mode by invoking the start() method and it can be killed by using stop() method.
2 Runnable State
It means that thread is now ready for running and its waiting to give control.
We can move control to another thread by yield() method.
3 Running State
It means thread is in its execution mode becaause the control of cpu is given to that particular thread.
It can be move in three different situation from running mode.
These all are different methods which can be apply on running thread and how the state is changing and how we can come in our original previous state using different methods are shown in above figure.
4 Blocked State
A thread is called in Blocked State when it is not allowed to entering in Runnable State or Running State.
It happens when thread is in waiting mode, suspended or in sleeping mode.
5 Dead State
When a thread is completed executing its run() method the life cycle of that particular thread is end.
We can kill thread by invoking stop() method for that particular thread and send it to be in Dead State.