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 for the number of seconds specified by the time attribute. Write a message in the console indicating the end of phase one. And, synchronize with the rest of the tasks using the arriveAndAwaitAdvance() method of the phaser attribute:
System.out.printf("%s: Entering phase 1. ",
Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("%s: Finishing phase 1. ",
Thread.currentThread().getName());
phaser.arriveAndAwaitAdvance();
- Repeat this behavior in both second and third phases. At the end of the third phase, use the arriveAndDeregister() method instead of arriveAndAwaitAdvance():
System.out.printf("%s: Entering phase 2. ",
Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("%s: Finishing phase 2. ",
Thread.currentThread().getName());
phaser.arriveAndAwaitAdvance();
System.out.printf("%s: Entering phase 3. ",
Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("%s: Finishing phase 3. ",
Thread.currentThread().getName());
phaser.arriveAndDeregister();
- Implement the main class of the example by creating a class named Main with a main() method:
public class Main {
public static void main(String[] args) throws Exception {
- Create a new Phaser object named phaser with three participants:
Phaser phaser=new Phaser(3);
- Create and launch three threads to execute three task objects:
for (int i=0; i<3; i++) {
Task task=new Task(i+1, phaser);
Thread thread=new Thread(task);
thread.start();
}
- Create a loop with 10 steps to write information about the phaser object:
for (int i=0; i<10; i++) {
- Write information about the registered parties, the phase of the phaser, the arrived parties, and the unarrived parties:
System.out.printf("******************** ");
System.out.printf("Main: Phaser Log ");
System.out.printf("Main: Phaser: Phase: %d ",
phaser.getPhase());
System.out.printf("Main: Phaser: Registered Parties: %d ",
phaser.getRegisteredParties());
System.out.printf("Main: Phaser: Arrived Parties: %d ",
phaser.getArrivedParties());
System.out.printf("Main: Phaser: Unarrived Parties: %d ",
phaser.getUnarrivedParties());
System.out.printf("******************** ");
- Put the thread to sleep for 1 second and close the loop and the class:
TimeUnit.SECONDS.sleep(1);
}
}
}