Type a volatile stop flag
Type a volatile stop flag
Answer
private volatile boolean running = true; public void stop() { running = false; }
volatile guarantees the worker thread sees the latest value of running, so the spin loop terminates when stop() is called.