Complete example for use of volatile key word in Java?
I need a simple example of use of the volatile keyword in Java, behaving inconsistently as a result of not using volatile . The theory part of volatile usage is already clear to me. aioobe First of all, there's no guaranteed way of exposing caching due to non-volatile variables. Your JVM might just be very kind to you all the time and effectively treat every variable as volatile. That being said, there are a few ways to increase probability of having threads caching their own versions of a non-volatile variable. Here is a program that exposes the importance of volatile in most machines I've