Suppose that I have the following classes
Looper.java
class Looper {
boolean stop;
void loop() {
while(!stop) {
// do something
}
}
void stop() {
stop = true;
}
}
Launcher.java
class Launcher {
public static void main(String[] args) {
Looper looper = new Looper();
new Thread(() -> looper.loop()).start();
new Thread(() -> looper.stop()).start();
}
}
Is it legal for the compiler/JIT/CPU to transform this Looper
class into the following manner such that the loop never ends?
class Looper {
boolean stop;
void loop() {
while(true) { // as stop is always false from the the thread that enters this method
// do something
}
}
void stop() {
stop = true;
}
}