On my machine the following code runs indefinitely (Java 1.7.0_07):
public static void main(String[] args) throws InterruptedException {
Thread backgroundThread = new Thread(new Runnable() {
public void run() {
int i = 0;
while (!stopRequested) {
i++;
}
}
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
stopRequested = true;
}
However, add a single lock object and a single synchronized
statement NOT around stopRequested
(in fact, nothing occurs in the synchronized block), and it terminates:
public static void main(String[] args) throws InterruptedException {
Thread backgroundThread = new Thread(new Runnable() {
public void run() {
Object lock = new Object();
int i = 0;
while (!stopRequested) {
synchronized (lock) {}
i++;
}
}
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
stopRequested = true;
}
In the original code, the variable stopRequested
is "hoisted", becoming:
if (!stopRequested)
while (true)
i++;
However, in the modified version, it seems this optimization is not occurring, why? (In fact, why is synchronized
not optimized away entirely?)