How to do it...

Follow these steps to implement the example:

  1. Create a class named Task that implements the Runnable interface:
        public class Task implements Runnable {
  1. Declare a private int attribute named time:
        private final int time;
  1. Declare a private Phaser attribute named phaser:
        private final Phaser phaser;
  1. Implement the constructor of the class to initialize its attributes:
        public Task(int time, Phaser phaser) {           this.time=time;           this.phaser=phaser;         }
  1. Implement the run() method. First, instruct the phaser attribute that the task starts its execution with the arrive() method:
        @Override         public void run() {            phaser.arrive();
  1. Write a message in the console indicating the start of phase one. Put the thread to sleep ...

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.