Follow these steps to implement the example:
- Create a class named Task that implements the Runnable interface:
public class Task implements Runnable {
- Declare a private int attribute named time:
private final int time;
- Declare a private Phaser attribute named phaser:
private final Phaser phaser;
- Implement the constructor of the class to initialize its attributes:
public Task(int time, Phaser phaser) { this.time=time; this.phaser=phaser; }
- 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();
- Write a message in the console indicating the start of phase one. Put the thread to sleep ...