Multithreading: interrupt() wakes a sleeping thread with an exception

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.

Code
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]);
Output
Worker was interrupted: true
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