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, begin by implementing the producer. Create a class named Producer and specify that it implements the Runnable interface:
        public class Producer implements Runnable {
  1. Declare a List<String> field named buffer. This will be the data structure that the producer will interchange with the consumer:
        private List<String> buffer;
  1. Declare an Exchanger<List<String>> field named exchanger. This will be the exchanger object that will be used to synchronize the producer and consumer:
        private final Exchanger<List<String>> exchanger;
  1. Implement the constructor of the class that will initialize the two attributes:
 public Producer (List<String> buffer, Exchanger<List<String>> ...

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