O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required