Skip to main content

Volatile Keyword

public class Counter {
private volatile boolean running = true;

public void stopRunning() {
running = false;
}

public void run() {
while (running) {
System.out.println("Thread is running...");
}
System.out.println("Thread is stopped.");
}
}
public class Main {
public static void main(String[] args) {
Counter counter = new Counter();

Thread thread = new Thread(counter::run);
thread.start();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

counter.stopRunning();
}
}