Thread join Timeout Then isAlive Check

join(millis) returns silently on timeout without telling you which case happened: follow it with isAlive() to distinguish finished from still running.

JavaResilience

Code

snippet
public class WorkerHandover {

    public static void main(String[] args) throws InterruptedException {
        Thread loader = new Thread(() -> {
            try {
                Thread.sleep(300); // 模拟耗时加载
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
        loader.start();

        // join(millis) 超时后静默返回:不抛异常、没有返回值、不告知完成与否
        loader.join(50);
        System.out.println("after join(50) isAlive: " + loader.isAlive());

        // 不带超时的 join:等到线程真正终止才返回
        loader.join();
        System.out.println("after join()  isAlive: " + loader.isAlive());
    }
}

The code in one pass

Under the hood: the class WorkerHandover.

Watch out for

  • Copy, run: the standard library answers every call in the file.

Putting it to use

Look for the after join(50) isAlive:, after join() isAlive: prefixes during a run of the file; each prefix keeps its line tied to a step.

Core lines

Thread.currentThread().interrupt();

Interactive Tools for This Topic

FAQ

What does Thread join Timeout Then isAlive Check print when run?
Run it as a complete program — the output lines carry labels such as `after join(50) isAlive:`, `after join() isAlive:`.