Thread Lifecycle in Java

A thread’s life starts when the method start() is invoked on it. Subsequently, it goes through various states before it finishes its task and is considered dead. Upon calling the start() method, the thread does not start running immediately. The start() method puts it into the runnable state, also called the ready state. It stays in the runnable state until the scheduler puts it into the running state. When the thread goes into the running state, the method run() is called.

Remember that: When the start() method is invoked on a thread, it does not start running immediately; it has to go through a scheduler.

Even during the execution of the thread run() method, the thread may temporarily stop executing and go into one of the nonrunning states, and eventually come back to the running state. The various states of a thread are listed here:

• New: This is the state of a thread when it has been instantiated but not yet started.

• Ready/runnable: A thread enters the runnable state for the first time when the start() method is invoked on the thread instance. Later, the thread can come back to this state from one of the nonrunnable states. In the runnable state, the thread is ready to run and waiting to be selected by the scheduler for running.

• Running: This is the state in which the thread is executing.



Non-runnable states of thread: A thread in the running state may go into one of the three nonrunnable states when it is still alive but not eligible to run. These three states are listed here:
  • Blocked: A thread goes into this state when it’s waiting for a resource such as I/O or anobject’s lock. The availability of the resource will send it back to the runnable state. You will learn about the object’s lock later in this chapter. 
  • Sleeping: This state is one of the timed waiting states because the thread stays in this state for a specific time. A thread goes into this state when its code tells it to sleep for a specific period of time by calling the sleep(…) method. Expiration of the sleep time sends it back to the runnable state.
  • Waiting: The thread goes into the waiting state when the object on which it’s running invokes the wait() method. A call to notify() or notifyAll() (by another thread) will bring it back to the runnable state.
• Dead: A thread is considered dead after the execution of its run() method is complete. A dead thread can never be run as a separate thread again—that is, if you call its start() method, you will receive a runtime exception. However, it is still an object, and you can still call its methods (other than the start() method), but they will be executed in the caller’s thread.

Below figure shows the relationship among all these states of a thread. A running thread may go into blocked or sleeping state and wait there for an event to happen. When the right event happens, the thread goes to the ready state and eventually is put into the running state again by the scheduler.

Different states of a thread


Reference(s): SCJP Exam for J2SE 5 

No comments:

Post a Comment