Calling interrupt() on a thread that is blocked in sleep(), wait() or join() makes that call throw InterruptedException immediately instead of waiting out the full duration. It is the cooperative way to ask a thread to stop early.
boolean[] interrupted = new boolean[1];
Thread worker = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
interrupted[0] = true;
}
});
worker.start();
Thread.sleep(20);
worker.interrupt();
worker.join();
System.out.println("Worker was interrupted: " + interrupted[0]);
Worker was interrupted: true
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