At any point during execution, every Java thread resides in exactly one of the six states defined in the java.lang.Thread.State enum.
Figure 11: Java Thread Lifecycle: Transition triggers between NEW, RUNNABLE, BLOCKED, WAITING, and TERMINATED.
new Thread()), but start() has not yet been called.synchronized block or method.Object.wait() or Thread.join() without timeout).Thread.sleep(1000), wait(5000)).run() method or was aborted by an uncaught exception.Thread.join()public class ThreadJoinDemo {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
System.out.println("Worker: Downloading large database backup...");
try { Thread.sleep(2000); } catch (Exception e) {}
System.out.println("Worker: Download finished!");
});
worker.start();
System.out.println("Main: Waiting for worker to finish...");
worker.join(); // Main thread pauses until worker terminates!
System.out.println("Main: Resuming. All data downloaded successfully.");
}
}
When multiple threads access shared mutable state, race conditions occur. Let us explore Thread Synchronization and Monitor Locks. Proceed to: Java Thread Synchronization →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Creating threads with Thread/Runnable.
Monitor locks and BLOCKED state.
Transitioning out of WAITING.
Unmounting virtual threads.