How to do it...

Follow these steps to implement the example:

  1. Create a class named MyPhaser and specify that it extends from the Phaser class:
        public class MyPhaser extends Phaser {
  1. Override the onAdvance() method. According to the value of the phase attribute, we call it a different auxiliary method. If the phase attribute is equal to zero, you have to call the studentsArrived() method. If the phase is equal to one, you have to call the finishFirstExercise() method. If the phase is equal to two, you have to call the finishSecondExercise() method. Finally, if the phase is equal to three, you have to call the finishExam() method. Otherwise, return the true value to indicate that phaser has terminated:
        @Override 
protected boolean onAdvance(int phase, int registeredParties) {
switch (phase) {
case 0:
return studentsArrived();
case 1:
return finishFirstExercise();
case 2:
return finishSecondExercise();
case 3:
return finishExam();
default:
return true;
}
}
  1. Implement the auxiliary method studentsArrived(). It writes two log messages to the console and returns false to indicate that phaser is continuing with its execution:
        private boolean studentsArrived() { 
System.out.printf("Phaser: The exam are going to start.
The students are ready. ");
System.out.printf("Phaser: We have %d students. ",
getRegisteredParties());
return false;
}
  1. Implement the auxiliary method finishFirstExercise(). It writes two messages to the console and returns false to indicate that phaser is continuing with its execution:
        private boolean finishFirstExercise() { 
System.out.printf("Phaser: All the students have finished the
first exercise. ");
System.out.printf("Phaser: It's time for the second one. ");
return false;
}
  1. Implement the auxiliary method finishSecondExercise(). It writes two messages to the console and returns false to indicate that phaser is continuing with its execution:
        private boolean finishSecondExercise() { 
System.out.printf("Phaser: All the students have finished the
second exercise. ");
System.out.printf("Phaser: It's time for the third one. ");
return false;
}
  1. Implement the auxiliary method finishExam(). It writes two messages to the console and returns true to indicate that phaser has finished its work:
        private boolean finishExam() { 
System.out.printf("Phaser: All the students have finished
the exam. ");
System.out.printf("Phaser: Thank you for your time. ");
return true;
}
  1. Create a class named Student and specify that it implements the Runnable interface. This class will simulate the students of an exam:
        public class Student implements Runnable {
  1. Declare a Phaser object named phaser:
        private Phaser phaser;
  1. Implement the constructor of the class that initializes the Phaser object:
        public Student(Phaser phaser) { 
this.phaser=phaser;
}
  1. Implement the run() method that will simulate the realization of the exam:
        @Override 
public void run() {
  1. First, the method writes a message in the console to indicate that a student has arrived at the exam hall and calls the arriveAndAwaitAdvance() method of phaser to wait for the rest of the threads:
        System.out.printf("%s: Has arrived to do the exam. %s
",
Thread.currentThread().getName(),new Date());
phaser.arriveAndAwaitAdvance();
  1. Then, write a message to the console and call the private doExercise1() method that simulates the realization of the first exercise of the exam. Post this, write another message to the console and the arriveAndAwaitAdvance() method of phaser to wait for the rest of the students to finish the first exercise:
        System.out.printf("%s: Is going to do the first exercise.%s
",
Thread.currentThread().getName(),new Date());
doExercise1();
System.out.printf("%s: Has done the first exercise.%s ",
Thread.currentThread().getName(),new Date());
phaser.arriveAndAwaitAdvance();
  1. Implement the same code for the second and third exercises:
        System.out.printf("%s: Is going to do the second exercise. 
%s ",Thread.currentThread().getName(),
new Date());
doExercise2();
System.out.printf("%s: Has done the second exercise.%s ",
Thread.currentThread().getName(),new Date());
phaser.arriveAndAwaitAdvance();
System.out.printf("%s: Is going to do the third exercise.%s ",
Thread.currentThread().getName(),new Date());
doExercise3();
System.out.printf("%s: Has finished the exam.%s ",
Thread.currentThread().getName(),new Date());
phaser.arriveAndAwaitAdvance();
  1. Implement the auxiliary method doExercise1(). This method puts the current thread or the thread that executes the method to sleep for a random period of time:
        private void doExercise1() { 
try {
long duration=(long)(Math.random()*10);
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Implement the auxiliary method doExercise2(). This method puts the current thread or the thread that executes the method to sleep for a random period of time:
        private void doExercise2() { 
try {
long duration=(long)(Math.random()*10);
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Implement the auxiliary method doExercise3(). This method puts the thread to sleep for a random period of time:
        private void doExercise3() { 
try {
long duration=(long)(Math.random()*10);
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Implement the main class of the example by creating a class named Main and adding the main() method to it:
        public class Main { 
public static void main(String[] args) {
  1. Create a MyPhaser object:
        MyPhaser phaser=new MyPhaser();
  1. Create five Student objects and register them in the phaser attribute using the register() method:
        Student students[]=new Student[5]; 
for (int i=0; i<students.length; i++){
students[i]=new Student(phaser);
phaser.register();
}
  1. Create five threads to run students and start them:
        Thread threads[]=new Thread[students.length]; 
for (int i=0; i<students.length; i++){
threads[i]=new Thread(students[i],"Student "+i);
threads[i].start();
}
  1. Wait for the finalization of the five threads:
        for (int i=0; i<threads.length; i++){ 
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Write a message to show that phaser is in the termination state, using the isTerminated() method:
        System.out.printf("Main: The phaser has finished: %s.
",
phaser.isTerminated());
..................Content has been hidden....................

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