O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required