How to do it...

Follow these steps to implement the example:

  1. First, implement the shared data object. Create a class named Position with two integer attributes, namely x and y. You have to include the methods to get and set the values of the attributes. Its code is very simple so it is not included here.
  2. Now let's implement the Writer task. It implements the Runnable interface and it will have two attributes: a Position object named position and StampedLock named lock. They will be initialized in the constructor:
         public class Writer implements Runnable {            private final Position position;           private final StampedLock lock;            public Writer (Position position, StampedLock lock) {             this.position=position;             this.lock=lock;           }
  1. Implement the ...

Get Java 9 Concurrency Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.