How to do it...

Follow these steps to implement the example:

  1. Create a class named Flag with a public Boolean attribute named flag initialized to the true value:
        public class Flag {           public boolean flag=true;         }
  1. Create a class named VolatileFlag with a public Boolean attribute named flag initialized to the true value. We add the volatile modifier to the declaration of this attribute:
        public class VolatileFlag {           public volatile boolean flag=true;         }
  1. Create a class named Task and specify that it implements the Runnable interface. It has a private Flag attribute and a constructor to initialize it:
        public class Task implements Runnable {           private Flag flag;           public Task(Flag flag) {             this.flag = flag;           }
  1. Implement the run() method ...

Get Java 9 Concurrency Cookbook - 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.