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 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.