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 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();
  1. 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();
  1. 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 {
  1. Create a new Phaser object named phaser with three participants:
        Phaser phaser=new Phaser(3);
  1. 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();
}
  1. Create a loop with 10 steps to write information about the phaser object:
        for (int i=0; i<10; i++) {
  1. 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("******************** ");
  1. Put the thread to sleep for 1 second and close the loop and the class:
              TimeUnit.SECONDS.sleep(1); 
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.204.162