Variable access

Now that we can start threads and create code that runs parallel, it is time to talk a little bit about how these threads can exchange data between one another. At first glance, it seems fairly simple. The threads use the same shared memory; therefore, they can read and write all the variables that the Java access protection allows them. This is true, except that some threads may just decide not to read the memory. After all, if they have just recently read the value of a particular variable, why read it again from the memory to the registers if it was not modified? Who would have modified it? Let's see the following short example:

package packt.java189fundamentals.thread; public class VolatileDemonstration implements Runnable ...

Get Java Projects - Second Edition 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.