capint
2/7/2017 - 4:48 PM

Java >> Thread >> volatile

Java >> Thread >> volatile

If two threads are both reading and writing to a shared variable, then using the volatile keyword for that is not enough => Need to use synchronized or AtomicReference to avoid memory consistency errors.
Source: http://tutorials.jenkov.com/java-concurrency/volatile.html

By declaring a variable volatile all writes to the variable will be written back to main memory immediately. Also, all reads of the variable will be read directly from main memory => Avoid the problem of visibility.