The volatile data

Finally, to conclude this part of the Java Standalone concurrent programming, it is necessary to keep in mind why the volatile keyword is important. This keyword allows you to request the JVM to refresh the value it reads every time it accesses the data.

It is very simple to use; just add the volatile keyword on the field declaration:

public class SomeData {    private volatile int value;}

To understand why this keyword changes everything, you need to keep in mind that the JVM can have some thread-related caching of the field values (this is very low-level caching and has nothing to do with what we'll see in the next section). Adding this keyword as in the previous snippet forces us to bypass this cache. It is supposed to be ...

Get Java EE 8 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.