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

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