The code is not thread safe because the current thread can see the changes until the code is compiled (which may be at a random point later) and you no longer see the changes.
BTW: This makes testing very difficult. for example, if you sleep for 1 second, you may not see this behavior for almost three hours.
i.e. it may or may not work, and you cannot say only because it worked, it will continue to work.
Since b not volatile , JIT can and will optimize
while (b) Thread.sleep(N);
to be
boolean b = this.b; if (b) while (true) Thread.sleep(N);
therefore, the value of b not read every time.
source share