Exceptions: catching InterruptedException should restore the thread's interrupt status

A thread's interrupted flag is cleared the instant InterruptedException is thrown, so a handler that only logs and moves on hides the interruption from the rest of the code; re-calling interrupt() restores that flag.

Code
Thread worker = new Thread(() -> {
    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        System.out.println("Caught: " + e.getClass().getSimpleName());
        Thread.currentThread().interrupt();
    }
    System.out.println("Interrupted flag restored: " + Thread.currentThread().isInterrupted());
});
worker.start();
worker.interrupt();
worker.join();
Output
Caught: InterruptedException
Interrupted flag restored: 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