Brief IntroductionJava Multithreading Technology provides the Phaser tool class, Phaser represents the "stage", which is used to solve scenarios that control multiple threads accomplishing tasks in phases. Its role compared to countdownlatch and cyclicbarrier more flexible, such as a topic: 5 students to participate in the examination, a total of three questions, requiring all students to start the exam, all students finish the first question, students can continue to do the second question, All the students finished the second question, can do the third question, all students have finished the third question, the examination only then ends. Analysis of this topic: This is a multithreaded (5 students) phased problem (test exams, the first problem is done, the second problem is done, the third problem is done), so it is suitable to use phaser to solve this problem.
Implementation Code
Import Java.util.concurrent.Phaser;
/*** * Below say the advanced usage of phaser, there are 2 important states in Phaser, respectively, phase and party. * Phase is the stage, the initial value is 0, when all the threads perform this task, and start the next round of tasks, * means that the current phase is over, go to the next stage, the value of phase automatically add 1. The party is the thread, * party=4 means that the Phaser object currently manages 4 threads. Phaser also has an important method that often needs to be overloaded, * that's the boolean onadvance (int phase, int registeredparties) method.
This method has 2 effects: * 1, when each phase is completed, this method is invoked automatically, so the code that overloads this method is executed at the end of each phase, * equivalent to the Cyclicbarrier barrieraction.
* 2, when this method returns true, it means that the phaser is terminated, so you can subtly set the return value of this method to terminate all threads. * @author Liujun */public class Myphaser extends Phaser {@Override protected boolean onadvance (int phase, int regist
Eredparties) {//the method switch (phase) {case 0:return studentarrived () in which the callback is performed after completion of each phase;
Case 1:return finishfirstexercise ();
Case 2:return finishsecondexercise ();
Case 3:return Finishexam ();
Default:return true;
} private Boolean studentarrived () {System.out.println ("Students are ready, number of students:" +getregisteredparties ());
return false; Private Boolean finishfirstexercise () {SysteM.out.println ("First question all students finish");
return false;
Private Boolean finishsecondexercise () {System.out.println ("Second question all students finish");
return false;
Private Boolean Finishexam () {System.out.println ("Third question all students finish, end the exam");
return true; }
}
Import Java.util.concurrent.Phaser;
Import Java.util.concurrent.TimeUnit;
public class Studenttask implements Runnable {private Phaser phaser;
Public Studenttask (Phaser phaser) {this.phaser = Phaser;
@Override public void Run () {System.out.println () Thread.CurrentThread (). GetName () + "arrival test");
Phaser.arriveandawaitadvance ();
System.out.println (Thread.CurrentThread (). GetName () + "Do 1th Question Time ...");
DoExercise1 ();
System.out.println (Thread.CurrentThread (). GetName () + "complete the 1th question ...");
Phaser.arriveandawaitadvance ();
System.out.println (Thread.CurrentThread (). GetName () + "Do 2nd Question Time ...");
DoExercise2 ();
System.out.println (Thread.CurrentThread (). GetName () + "complete the 2nd question ...");
Phaser.arriveandawaitadvance ();
System.out.println (Thread.CurrentThread (). GetName () + "Do 3rd Question Time ...");
DOEXERCISE3 ();
System.out.println (Thread.CurrentThread (). GetName () + "complete the 3rd question ...");
Phaser.arriveandawaitadvance ();
private void DoExercise1 () {Long duration = (long) (Math.random () *10);
try { TimeUnit.SECONDS.sleep (duration);
catch (Interruptedexception e) {e.printstacktrace ();
} private void DoExercise2 () {Long duration = (long) (Math.random () *10);
try {TimeUnit.SECONDS.sleep (duration);
catch (Interruptedexception e) {e.printstacktrace ();
} private void DoExercise3 () {Long duration = (long) (Math.random () *10);
try {TimeUnit.SECONDS.sleep (duration);
catch (Interruptedexception e) {e.printstacktrace ();
}
}
}
/** * Topic: 5 Students take the examination, a total of three questions, requiring all students to start the exam *, all finished the first question, to continue to do the second question, followed by similar.
* * Phaser has phase and party two important states, * phase stage, the party represents the number of threads per phase, * Only each thread has executed the phaser.arriveandawaitadvance ();
* Will enter the next stage, otherwise blocking waiting. * For example, 5 students (threads) in the title use Phaser.arriveandawaitadvance (); Go to the next question * @author Liujun * * public class Main {public static void Mai
N (string[] args) {Myphaser phaser = new Myphaser ();
studenttask[] Studenttask = new STUDENTTASK[5];
for (int i = 0; i < studenttask.length i++) {Studenttask[i] = new Studenttask (phaser); Phaser.register ();
Register once represents the number of threads Phaser maintenance} thread[] threads = new Thread[studenttask.length];
for (int i = 0; i < studenttask.length i++) {threads[i] = new Thread (studenttask[i), "Student" +i);
Threads[i].start ();
//wait for all threads to execute end for (int i = 0; i < studenttask.length; i++) {try {threads[i].join ();
catch (Interruptedexception e) {e.printstacktrace (); } System.out.println ("Phaser has finished:" +phaser.isterminAted ()); }
}
Results
Student 0 Arrival Test
Student 1 Arrival Test
Student 4 Arrival Test
Student 2 Arrival Test
Student 3 Arrival Test
The students are ready for 5.
Student 2 To do the 1th Question Time ...
Student 0 To do the 1th Question Time ...
Student 1 To do the 1th Question Time ...
Student 4 To do the 1th Question Time ...
Student 3 To do the 1th Question Time ...
Student 2 completes the 1th question ...
Student 3 completes the 1th question ...
Student 1 completes the 1th question ...
Student 0 completes the 1th question ...
Student 4 completes the 1th question ...
The first question all the students have done
Student 3 To do the 2nd Question Time ...
Student 0 To do the 2nd Question Time ...
Student 4 To do the 2nd Question Time ...
Student 1 To do the 2nd Question Time ...
Student 2 To do the 2nd Question Time ...
Student 3 completes the 2nd question ...
Student 2 completes the 2nd question ...
Stud ENT 0 completes the 2nd question ...
Student 1 completes the 2nd question ...
Student 4 completes the 2nd question ...
The second question all students finish
Student 0 To do the 3rd Question Time ...
Student 3 To do the 3rd Question Time ...
Student 2 To do the 3rd Question Time ...
Student 4 To do the 3rd Question Time ...
Student 1 To do the 3rd Question Time ...
Student 1 completes the 3rd question ...
Student 0 completes the 3rd question ...
Student 2 completes the 3rd question ...
Student 3 completes the 3rd question ...
Student 4 completes the 3rd question ...
The third question all the students finish, finish the exam
Phaser has Finished:true