Stoian Azarov wrote:I have done some more testing and it seems that my scenario is impossible.
The JVM spec goes into a ton of detail of how atomic instructions are (along what sorts of optimizations can happen when dealing with volatile variables and synchronization blocks).
Basically, getting an int is atomic, setting an int is atomic, so.... getting the value of x to use is atomic, the line "x=10" is atomic, but "x = x + 10" which both gets and sets the variable x, is *not* atomic.
The variable x is not declared as volatile, nor is synchronization used, some optimizations, such as caching it to a register may also be used. This second case, likely doesn't have any affect, as there is no need to cache unless you intend to reused, such as in a loop.
So... it is possible to end up with the variable x with a value of 10.
Henry