Understanding Thread Execution Without join
What you'll learn: You'll discover why spawned threads continue running independently and how your main thread can finish before worker threads complete their tasks.
The Independence Problem
When you call start() on a thread, it launches and runs independently of the thread that created it. The main thread doesn't automatically wait for worker threads to finish—it keeps executing its own code and will terminate as soon as its own work is done.
Think of it like this: you're a manager (main thread) who assigns tasks to employees (worker threads). Once you've given out the assignments and finished your own paperwork, you might leave the office—even if your employees are still working on their tasks. The employees continue their work whether you're there or not.
What Happens in Practice
public class IndependentThreads {
public static void main(String[] args) {
Thread worker = new Thread(() -> {
for (int i = 1; i <= 5; i++) {
System.out.println("Worker: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
worker.start();
System.out.println("Main thread finished!");
// main exits immediately, but worker keeps running
}
}
You'll see "Main thread finished!" print almost immediately, while the worker thread continues counting to 5 in the background. The JVM only terminates when all non-daemon threads complete—not when main ends.
The Consequence
Without coordination, you lose control over execution order. Main might finish before workers produce results you need, leading to unpredictable program behavior.
Key Takeaway: Threads run independently after start(); the main thread finishing doesn't stop worker threads, and you have no guarantee about execution order without explicit coordination mechanisms.