Follow these steps to implement the example:
- Create a class named Flag with a public Boolean attribute named flag initialized to the true value:
public class Flag { public boolean flag=true; }
- 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; }
- 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; }
- Implement the run() method ...