Advance Java Features - Multithreading: Thread States

Comprehensive study notes, diagrams, and exam preparation for Advance Java Features - Multithreading: Thread States.

Advance Java Features - Multithreading: Thread States

Definition

In Java, the lifecycle of a thread is managed by the Java Virtual Machine (JVM). Thread states represent the specific condition or stage in which a thread exists at any given point during its execution. These states are defined by the Thread.State enum in the java.lang.Thread class, ensuring that the JVM can efficiently manage CPU time and resources across multiple concurrent tasks.


Main Content

1. New State

  • A thread is in the New state when it is created using the Thread class but the start() method has not been invoked yet.
  • At this point, the thread object exists in the heap memory, but the underlying OS-level thread has not yet been initialized.

2. Runnable State

  • Once the start() method is called, the thread moves to the Runnable state.
  • In this state, the thread is ready to run and is waiting for its turn to get CPU time from the thread scheduler. It may be currently executing or waiting for more execution time.

3. Blocked and Waiting States

  • Blocked: A thread enters this state when it is trying to access a synchronized block or method that is currently locked by another thread.
  • Waiting/Timed Waiting: A thread enters this state when it is waiting for another thread to perform a specific action (like notify() or join()). Timed Waiting occurs when a thread waits for a specified period (e.g., Thread.sleep(1000)).

Working / Process

1. Thread Initialization

  • The programmer creates a new instance of a class that extends Thread or implements Runnable.
  • The thread remains in the "New" state until the programmer explicitly triggers the lifecycle by calling thread.start().

2. Thread Scheduling

  • The Java Thread Scheduler moves the thread into the "Runnable" state.
  • If the CPU is available, the scheduler assigns processing cycles to the thread, moving it to the "Running" sub-stage of the Runnable state.

3. Termination

  • Once the run() method finishes execution or an unhandled exception occurs, the thread transitions to the "Terminated" (or Dead) state.
  • Once a thread is terminated, it cannot be restarted.
[New] --start()--> [Runnable] <---> [Running]
                      |    ^
                      |    | (I/O or Lock)
                      v    |
                  [Waiting/Blocked]
                      |
                      v
                 [Terminated]

Advantages / Applications

  • Responsiveness: By managing thread states, UI applications can stay responsive while performing background tasks.
  • Resource Optimization: The JVM pauses threads (Waiting/Blocked) that cannot proceed, freeing up the CPU for threads that are ready to work.
  • Asynchronous Processing: Thread states allow Java to handle thousands of concurrent requests in web servers by efficiently switching between active and waiting threads.

Summary

Multithreading in Java refers to the concurrent execution of two or more parts of a program to maximize CPU utilization. The thread lifecycle moves through specific states: New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated. Efficient state management is critical for developing scalable, high-performance Java applications.

Important terms to remember: Thread.State, Thread Scheduler, Runnable, Blocked, and Terminated.