Spot the bug in the virtual-thread task

from Java 21 features
Java 25 LTS intermediate 4 min 1 issue to find

Find the cancellation bug in this virtual-thread task.

Java
public class StockWait {
    static void waitForStock() {
        try {
            Thread.sleep(1_000);
        } catch (InterruptedException ignored) {
            System.out.println("retrying");
        }
    }

    public static void main(String[] args) throws Exception {
        var worker = Thread.startVirtualThread(StockWait::waitForStock);
        worker.interrupt();
        worker.join();
    }
}
Open in playground
Report an error