Java Multitasking  «Prev  Next»

Lesson 7The life cycle of a thread
ObjectiveList the states of a thread in Java

Java Thread Life Cycle

Question: Describe the life cycle of a thread in Java 1.1
In Java 1.1, the life cycle of a thread can be divided into five distinct stages: new, runnable, running, blocked, and terminated.
  1. New: When a new thread object is created, it is in the new state. At this stage, the thread is not yet started and has not yet been allocated any system resources.
  2. Runnable: After the start() method is called on the thread object, it enters the runnable state. At this stage, the thread is ready to run and is waiting for the JVM to allocate processor time to it.
  3. Running: When the JVM allocates processor time to the thread, it enters the running state. At this stage, the thread is actively executing its code.
  4. Blocked: Sometimes, a thread may be temporarily blocked from running due to various reasons, such as waiting for I/O operations to complete or waiting for another thread to release a lock. In such cases, the thread enters the blocked state.
  5. Terminated: When the thread finishes executing its code or when the stop() method is called on it, it enters the terminated state. At this stage, the thread is no longer running and all of its system resources have been released.

It is important to note that while the above five states represent the general life cycle of a thread in Java 1.1, the exact implementation and behavior may vary depending on the specific JVM implementation and operating system. In addition, newer versions of Java may have different or additional thread states and behaviors.


Describe ways to move a thread in Java from one state to another using the Java 1.1 Threading Model

In Java 1.1 Threading Model, there are several ways to move a thread from one state to another. Let's take a look at some of them:
  1. Creating a Thread: The first way to move a thread into the new state is to create an instance of the Thread class. Once the thread is created, it is in the new state.
  2. Starting a Thread: After the thread is created, it can be moved into the runnable state by calling the start() method. When this method is called, the thread is added to the JVM's scheduling queue and will eventually be executed.
  3. Sleeping: While a thread is running, it can be moved from the running state to the blocked state by calling the sleep() method. This method causes the thread to stop executing for a specified amount of time, during which the thread is blocked.
  4. Waiting: A thread can be moved from the running state to the blocked state by calling the wait() method. This method causes the thread to release the lock it holds and wait for another thread to notify it. Once the thread is notified, it will reacquire the lock and move back into the runnable state.
  5. Blocking: A thread can also be moved from the running state to the blocked state by calling a method that blocks, such as read() or accept(). These methods cause the thread to wait for input or a connection, during which time it is blocked.
  6. Interrupting: A thread can be moved from the blocked state to the runnable state by calling the interrupt() method. This method interrupts the thread and causes it to leave its blocked state and move into the runnable state.
  7. Stopping: A thread can be moved from any state to the terminated state by calling the stop() method. This method forcefully terminates the thread and releases all of its system resources.

It is important to note that while these methods can be used to move a thread between states in Java 1.1 Threading Model, some of them, such as stop(), are considered unsafe and should be used with caution. In addition, newer versions of Java have different or additional methods for manipulating thread states. Threads run as part of the system and perform tasks simultaneously and it is difficult to know how they behave. In addition, Java threads have definite life cycles.

Thread Lifecycle

Let us start by looking at the lifecycle of an application thread. Every operating system has a view of threads that can differ in the details (but in most cases is broadly similar at a high level). Java tries hard to abstract these details away, and has an enum called Thread.State.
The values of Thread.State provide an overview of the lifecycle of a thread:

Thread life cycles

  1. NEW:The thread has been created but its start() method has not yet been called. All threads start in this state.
  2. RUNNABLE:The thread is running or is available to run when the operating system schedules it.
  3. BLOCKED:The thread is not running because it is waiting to acquire a lock so that it can enter a synchronized method or block. We will see more about synchronized methods and blocks later in this section.
  4. WAITING:The thread is not running because it has called Object.wait() or Thread.join().
  5. TIMED_WAITING - (Sleeping, Waiting): The thread is not running because it has called Thread.sleep() or has called Object.wait() or Thread.join() with a timeout value.
  6. TERMINATED: The thread has completed execution. Its run() method has exited normally or by throwing an exception.

Thread States in a Nutshell
States of a thread in Java