How to do it...

Follow these steps to implement the example:

  1. Create a class named Incrementer and specify that it implements the Runnable interface:
        public class Incrementer implements Runnable {
  1. Declare a private AtomicIntegerArray attribute named vector to store an array of integer numbers:
        private final AtomicIntegerArray vector;
  1. Implement the constructor of the class to initialize its attribute:
        public Incrementer(AtomicIntegerArray vector) {           this.vector=vector;         }
  1. Implement the run() method. Increment all the elements of the array using the getAndIncrement() method:
        @Override         public void run() {           for (int i=0; i<vector.length(); i++){             vector.getAndIncrement(i);           }         }
  1. Create a class named Decrementer and specify that ...

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.