This week's book giveaways are in the iOS and Features new in Java 8 forums. We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line! See this thread and this one for details.
0.109 Thread-0 entering run()
0.203 main entering workMethod()
0.203 main in workMethod() - about to sleep for 2 seconds
2.203 main in workMethod() - just set value=50
2.203 main in workMethod() - about to sleep for 5 seconds
2.203 Thread-0 leaving run() missedIt false value 50
7.203 main in workMethod() - just set missedIt=true
7.203 main in workMethod() - about to sleep for 3 seconds
10.203 main leaving workMethod()
Why does the below code not get executed ?
value (variable) is not volatile then how does it break out of the while loop when its value gets changed in workMethod ?
I am not exactly sure I know what your question here. But if I take this literally:
"value (variable) is not volatile then how does it break out of the while loop when its value gets changed in workMethod"
And without looking at your code (I did look at your code though), then I would have to respond: Data can get published between threads without volatile, so you could see value change in one thread as another thread updates it, even if it weren't volatile (and if there was nothing else going on). The difference between volatile and non-volatile is guarantees. With a volatile variable you are guaranteed to see the other thread's updates and you are guaranteed that data actions are performed in a 'happens-before' (and happens-after) relationship relative to the volatile variable, with non-volatile you are not guaranteed anything about timing or order.
"Why does the below code not get executed ? "
Because all of that is inside the if(missedIt) statement. It is apparent from the timestamps that the value is read as being changed prior to the missedIt flag being set.