Multithreading: Thread.join waits for a worker to finish

join() blocks the calling thread until the target thread has terminated, so any state the worker wrote is safely visible afterwards. Without it you could read the result before the worker ever set it.

Code
int[] result = new int[1];
Thread worker = new Thread(() -> result[0] = 6 * 7);
worker.start();
worker.join();
System.out.println("Answer: " + result[0]);
Output
Answer: 42
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms