Java Thread Life Cycle
The following diagram illustrates the various states that a Java thread can be in during its life and which method calls cause a transition from one state to another.
Figure 1: Life Cycle of a Java Thread
1. Newborn State
Whenever a new Thread object is created, it is said to be in a newborn state. At this state, the thread is not yet scheduled for running and we can do only one of the following things with it-
♦ Schedule it for running using start() method.
♦ Kill it using stop() method.
If scheduled, it moves to the runnable (ready-to-run) state. Attempting any other methods at this stage will cause an exception to be thrown.
2. Runnable (Ready-to-run) State:-
A newborn thread moves to runnable (i.e. ready-to-run) state with a call to start() method.
MyThread thread = new MyThread(); // create a new thread
thread.start(); // calling start() on this thread object
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 runnable threads based on thread priorities.
3. Running State
The thread code is being actively executed by the processor. It runs until it is swapped out, becomes blocked, or voluntarily gives up its turn with this static method Thread.yield().
It is to be noted 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.
4. Blocked State
A running thread can be moved to any one of the following blocked states:
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().
4. b) Sleeping
Java thread may be forced to sleep for some predefined time using:
It is to be noted 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 Thread.sleep(500) will cause the currently executing thread to sleep for 500 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 500 milliseconds.
4. c) Suspended
A running thread can be suspended using the suspend() method. A suspended thread can be revived by using the resume() method.
4. d) Blocked on I/O
A Java thread may enter this state while waiting for data from the I/O device. The thread will move to Ready-to-Run after I/O condition changes (such as reading a byte of data).
4. e) 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.
5. Dead State
A Java thread may enter this state when it is finished working. This is the natural death of a thread. However, we can kill a thread by calling the stop() method to it at any state thus causing a premature death to it. Once in this state, the thread cannot ever run again.
|⇒ The methods like suspend(), resume() and stop() have been deprecated. So it is better not to use them.|