移相器(Phaser)内有2个重要状态,分别是phase和party。
phase就是阶段,初值为0,当所有的线程执行完本轮任务,同时开始下一轮任务时,意味着当前阶段已结束,进入到下一阶段,phase的值自动加1。
party就是线程,party=4就意味着Phaser对象当前管理着4个线程。
Phaser还有一个重要的方法经常需要被重载,那就是boolean onAdvance(int phase, int registeredParties)方法。此方法有2个作用:
1、当每一个阶段执行完毕,此方法会被自动调用,因此,重载此方法写入的代码会在每个阶段执行完毕时执行,相当于CyclicBarrier的barrierAction。
2、当此方法返回true时,意味着Phaser被终止,因此可以巧妙的设置此方法的返回值来终止所有线程。
例如:若此方法返回值为 phase>=3,其含义为当整个线程执行了4个阶段后,程序终止。
Phaser把多个线程执行的任务划分成多个阶段(phase),编程时要明确各个阶段的任务,每个阶段都可以有任意个参与者,线程可以随时注册并参与到某个阶段,当一个阶段中所有线程都成功完成之后,Phaser的onAdvance()被调用,可以通过覆盖添加自定义处理逻辑(类似循环屏障(关卡)使用的Runnable接口),然后Phaser类会自动进入下个阶段。如此循环,直到Phaser不再包含任何参与者。
register(),bulkRegister(),动态添加一个或多个参与者。
arrive(),某个参与者完成任务后调用
arriveAndDeregister(),任务完成,取消自己的注册。
arriveAndAwaitAdvance(),自己完成等待其他参与者完成:进入阻塞,直到Phaser成功进入下个阶段。
awaitAdvance()、awaitAdvanceInterruptibly(),等待phaser进入下个阶段,参数为当前阶段的编号,后者可以设置超时和处理中断请求。
另外,Phaser的一个重要特征是多个Phaser可以组成树形结构,Phaser提供了构造方法来指定当前对象的父对象;当一个子对象参与者>0,会自动注册到父对象中;当=0,自动解除注册。

package org.suxuan;

import java.util.concurrent.Phaser;
import java.util.concurrent.atomic.AtomicReferenceArray; public class PhaserDemo {
public static void main(String[] args) {
final int workers = ;
final int workLength = ; final Phaser phaser = new Phaser(workers + );
final AtomicReferenceArray<String> lane1 = new AtomicReferenceArray<String>(new String[workLength]);
final AtomicReferenceArray<String> lane2 = new AtomicReferenceArray<String>(new String[workLength]); new Thread("Producer 1") {
@Override
public void run() {
for (int i = ; i < workLength; i++) {
$sleep(); lane1.set(i, "lane1-answer-" + i); System.out.printf("[%-17s] working in lane1 finished phase [%d]%n",
Thread.currentThread().getName(), phaser.getPhase()); phaser.arriveAndAwaitAdvance();
}
}
}.start(); new Thread("Slower producer 2") {
@Override
public void run() {
for (int i = ; i < workLength; i++) {
$sleep(); lane2.set(i, "lane2-answer-" + i); System.out.printf("[%-17s] working in lane2 finished phase [%d]%n",
Thread.currentThread().getName(), phaser.getPhase()); phaser.arriveAndAwaitAdvance();
}
}
}.start(); new Thread("Slow consumer") {
@Override
public void run() {
for (int start = ; start < workLength; ) {
System.out.printf("[%-17s] about to wait for phase [%d] completion%n",
Thread.currentThread().getName(), start); int phaseInProgress = phaser.awaitAdvance(start); //Read all the way up to the most recent completed phases.
for (int i = start; i < phaseInProgress; i++) {
System.out.printf("[%-17s] read [%s] & [%s] from phase [%d]%n",
Thread.currentThread().getName(), lane1.get(i), lane2.get(i), i);
} start = phaseInProgress; $sleep();
}
}
}.start(); phaser.arriveAndDeregister();
} private static void $sleep(long millis) {
try {
Thread.sleep(millis);
}
catch (InterruptedException e) {
}
}
}

Phaser初始化为3,之后customer线程取消自己的注册(此时只有两个生产者线程间进行同步),awaitAdvance不会阻塞,它直接返回。
两个生产者线程,依次生成结果放置到AtomicReferenceArray中。消费者线程每睡眠80Ms后,从结果集中,把当前已经完成的结果打印出来。
某次运行结果如下:

[Slow consumer    ] about to wait for phase [] completion
[Producer ] working in lane1 finished phase []
[Slower producer ] working in lane2 finished phase []
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Producer ] working in lane1 finished phase []
[Slower producer ] working in lane2 finished phase []
[Producer ] working in lane1 finished phase []
[Slow consumer ] about to wait for phase [] completion
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Slower producer ] working in lane2 finished phase []
[Producer ] working in lane1 finished phase []
[Slower producer ] working in lane2 finished phase []
[Producer ] working in lane1 finished phase []
[Slower producer ] working in lane2 finished phase []
[Producer ] working in lane1 finished phase []
[Slow consumer ] about to wait for phase [] completion
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Slower producer ] working in lane2 finished phase []
[Producer ] working in lane1 finished phase []
[Slower producer ] working in lane2 finished phase []
[Slow consumer ] about to wait for phase [] completion
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Slower producer ] working in lane2 finished phase []
[Producer ] working in lane1 finished phase []
[Producer ] working in lane1 finished phase []
[Slower producer ] working in lane2 finished phase []
[Producer ] working in lane1 finished phase []
[Slow consumer ] about to wait for phase [] completion
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
[Slower producer ] working in lane2 finished phase []
[Slow consumer ] about to wait for phase [] completion
[Slow consumer ] read [lane1-answer-] & [lane2-answer-] from phase []
05-06 12:01