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. Create a class named AddTask and specify that it implements the Runnable interface:
        public class AddTask implements Runnable {
  1. Declare a private ConcurrentLinkedDeque attribute parameterized by the String class named list:
        private final ConcurrentLinkedDeque<String> list;
  1. Implement the constructor of the class to initialize its attribute:
        public AddTask(ConcurrentLinkedDeque<String> list) {          this.list=list;         }
  1. Implement the run() method of the class. This method will have a loop with 5000 cycles. In each cycle, we will take the first and last elements of the deque so we will take a total of 10,000 elements:
        @Override         public void run() {  String name=Thread.currentThread().getName(); ...

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