How to do it...

Follow these steps to implement the example:

  1. Create a class named TaskAtomic and specify that it implements the Runnable interface:
        public class TaskAtomic implements Runnable {
  1. Declare a private AtomicInteger attribute named number:
        private final AtomicInteger number;
  1. Implement the constructor of the class to initialize its attributes:
        public TaskAtomic () {           this.number=new AtomicInteger();         }
  1. Implement the run() method. In a loop with 1,000,000 steps, assign the number of steps to the atomic attribute as a value, using the set() method:
        @Override         public void run() {           for (int i=0; i<1000000; i++) {             number.set(i);           }         }
  1. Create a class named TaskLock and specify that it implements the Runnable interface: ...

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.