Witscale Test Center

12.1 Thread basics > 12.1.4 The life cycle of thread and various thread states


12.1.4 The life cycle of thread and various thread states

The life of thread as a thread-of-execution begins when you instantiate a thread class and call start() method. It goes through various states before ultimately completing the job it is supposed to do. Figure 12.3 illustrates the various states a Java thread has to go through at various points in its life cycle.


 

Figure 12.3 various states a thread goes through from the moment it is started until it is dead

 

When the start() method is called, the thread enters in a ready-to-run state. This thread is now in the pool of threads ready for the execution. Sometime later the thread is picked up by the scheduler for execution and moved to the running state. This means that the thread can now execute its run() method.  The thread scheduler may move the thread out of the running state even if it has not finished the execution. In that case, the run method will be partially executed and the thread moves back to the ready-to-run state. Sometime later another thread from the ready-to-run threads’ pool is picked up by the scheduler and moved to the running state. This cycle continues, till the thread finishes executing its run() method. After that, it is moved to the dead state. All the threads in the threads pool go through this cycle to finish their execution.

Sometimes, The thread can also voluntary move out of running state. This happens when it is waiting on some event (as we will see later) and does not really need to be executed till that time. In such cases, it is moved to a not-ready-to-run state. After the event (for which thread is waiting) occurs, the thread moves back to the ready-to-run state. Let us see each of these states in detail.

Ready-to-run state

A thread is in ready-to-run (also known as runnable) state when it is eligible to run, but the scheduler has not yet picked it to actually run. The very first time a thread enters this state when its start() method is invoked for the first time. Note that a thread can also return to ready-to-run state either after running or after the not-ready-to-run (waiting for something else) state.

 

You should call the start() method only once on a thread object. Re-invoking a start method on thread object that is already started is illegal. You may get a runtime exception if you try to do so.

Running

All threads aspire to be in this state in their life cycle. When the thread enters in this state, the JVM starts to actively execute the thread’s code. The thread remains in this state and keep running  until it is either swapped out by thread scheduler or it voluntarily give up its turn for some reasons. We will see later various situations where the thread voluntarily leaves the running state. The state transitions in figure 12.3 shows several ways a thread gets out of the running state. Note that there is only one way to get in to the running state. That is when the scheduler chooses a thread from the threads pool.

Not-ready-to-run (Either in waiting, blocked or sleeping state)

A thread moves out of the running state (and goes to not-ready-to-run state) when it is waiting for something to happen. After the event for which it is waiting happens, the thread moves back to the ready-to-run state. Let us see briefly, when the thread can possibly enter the not-ready-to-run state:-

 

1.    Sleeping: You may want a thread to do-nothing for some time (may be until some other thread is finishes its job). In that case, you can call the  Thread.sleep() method in the thread’s run code. This method tells the currently running thread to sleep (do nothing) for some period of time. The thread wakes up when the stipulated time is elapsed and it goes back to the ready-to-run state.

 

2.    Waiting: Some times a thread might wait, just because you have asked it to wait (by calling the java.lang.Object's wait() method) in its run method. In that case, the thread changes its state from running to waiting (which is same as not-ready-to-run). The thread remains in this state until another thread sends a notification (by invoking the notify() or the notifyAll() method) that it is no longer necessary for the thread to wait.

 

3.    Blocked: Sometimes a thread needs to wait for a resource while running. For instance, if it is reading from a network resource in its run method, it has to wait until that resource becomes available. Until that time, the thread cannot do much and is not really running.  Therefore, it moves out of running state and enters the blocked state.  When the resource for which it is blocked becomes available, the thread again becomes eligible to execute and hence it moves to the ready-to-run state. 

 

There is a method called suspend() in the Thread class that lets one thread tell another to suspend its execution, but it is a deprecated method and will not be there for the exam. There is also a stop() method, but it also deprecated. Both suspend() and stop() turned out to be unsafe methods and therefore they are taken back. Another thing you need not worry about for the exam is the thread blocking on I/O (for example, waiting on bytes to arrive from an input stream). We will learn how a thread blocks when it is waiting to get an object’s lock later in this chapter.

 

Dead

A java thread enters this state when it has finished the execution of its run method. You cannot start the thread once it is dead. In other words, you cannot again bring it back to life. Note that the thread object continues to exist even after it enters the dead state. However, it ceases to exist as a thread-of-execution. Invoking the start method again on a thread also will not do any good. In fact, the thread can be started only once in its lifetime. If you re-invoke start() on a thread which is dead, it does not get started again. You can test whether the thread object is alive or dead (whether it has finished executing run), by invoking the isAlive() method on it.

 

After the start() method is invoked, the thread is becomes alive. It remains alive until it finishes the execution of its run method. The Thread class has isAlive() method that allows you to check whether the given thread is alive. Therefore invoking isAlive() on a thread object which is not started yet or which has finished its execution returns false.