How it works...

In this example, you implemented two different tasks to work with an AtomicIntegerArray object:

  • Incrementer: This class increments all the elements of the array using the getAndIncrement() method
  • Decrementer: This class decrements all the elements of the array using the getAndDecrement() method

In the Main class, you created AtomicIntegerArray with 1,000 elements, then you executed 100 incrementer and 100 decrementer tasks. At the end of these tasks, if there were no inconsistency errors, all the elements of the array must have the value 0. If you execute the program, you will see how the program only writes the final message to the console because all the elements are zero.

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.