Joining Threads

Thread Coordination - Part 2: Joining Threads 1. The Problem: The “Race” to the Finish When you start a thread, it runs independently. If the main thread needs the result of Thread-A to perform a calculation, main might finish before Thread-A even starts, leading to null results or crashes. 2. The Solution: thread.join() The join() method allows the calling thread (e.g., main) to go into a Waiting state until the target thread finishes its execution. ...

March 26, 2026

Thread Termination & Daemon Threads

Thread Termination & Daemon Threads 1. Why is Termination Tricky? Threads consume resources (Memory, CPU, File Handles). If a thread finishes its task but stays alive, it’s a Resource Leak. We cannot use the stop() method (it is deprecated and dangerous). We must use Cooperative Termination. 2. Method 1: The Interrupt Signal Interrupting is a way for one thread to signal another: “Please stop what you are doing.” Scenario A: Thread is Blocking (Sleeping/Waiting) If a thread is executing a method like Thread.sleep() or wait(), it will throw an InterruptedException when interrupted. ...

March 26, 2026