Phaser 的主要目的是实现代表一个或多个活动阶段的线程的同步。它让我们定义一个等待特定阶段完成的同步对象。然后它前进到下一阶段,直到该阶段结束。它还可以用于同步单个阶段,在这方面,它的作用很像 CyclicBarrier。
类层次结构
java.lang.Object ? java.util.concurrent ? Class Phaser
用法
public class Phaser extends Object
构造函数:
- Phaser()- 这将创建一个初始注册方为零的移相器。线程只有在注册后才能使用该移相器。
public Phaser()
- Phaser(各方)- 这将创建一个需要各方线程数才能进入下一阶段的移相器。
public Phaser(int parties) throws IllegalArgumentException
- Phaser(Phaser 父级)- 这指定了新对象的父移相器。注册方的数量设置为零。
public Phaser(Phaser parent)
- Phaser(Phaser 父级,int 方)- 这指定新创建的对象的父阶段器以及前进到下一阶段所需的参与方数量。
public Phaser(Phaser parent, int parties) throws IllegalArgumentException
示例 1:
注意:每次运行的输出可能会有所不同。
Java
// Java program to show Phaser Class
import java.util.concurrent.Phaser;
// A thread of execution that uses a phaser.
class MyThread implements Runnable {
Phaser phaser;
String title;
public MyThread(Phaser phaser, String title)
{
this.phaser = phaser;
this.title = title;
phaser.register();
new Thread(this).start();
}
@Override public void run()
{
System.out.println("Thread: " + title
+ " Phase Zero Started");
phaser.arriveAndAwaitAdvance();
// Stop execution to prevent jumbled output
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Thread: " + title
+ " Phase One Started");
phaser.arriveAndAwaitAdvance();
// Stop execution to prevent jumbled output
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Thread: " + title
+ " Phase Two Started");
phaser.arriveAndDeregister();
}
}
public class PhaserExample {
public static void main(String[] args)
{
Phaser phaser = new Phaser();
phaser.register();
int currentPhase;
System.out.println("Starting");
new MyThread(phaser, "A");
new MyThread(phaser, "B");
new MyThread(phaser, "C");
// Wait for all threads to complete phase Zero.
currentPhase = phaser.getPhase();
phaser.arriveAndAwaitAdvance();
System.out.println("Phase " + currentPhase
+ " Complete");
System.out.println("Phase Zero Ended");
// Wait for all threads to complete phase One.
currentPhase = phaser.getPhase();
phaser.arriveAndAwaitAdvance();
System.out.println("Phase " + currentPhase
+ " Complete");
System.out.println("Phase One Ended");
currentPhase = phaser.getPhase();
phaser.arriveAndAwaitAdvance();
System.out.println("Phase " + currentPhase
+ " Complete");
System.out.println("Phase Two Ended");
// Deregister the main thread.
phaser.arriveAndDeregister();
if (phaser.isTerminated()) {
System.out.println("Phaser is terminated");
}
}
}
输出
Starting Thread: B Phase Zero Started Thread: A Phase Zero Started Thread: C Phase Zero Started Thread: A Phase One Started Thread: B Phase One Started Thread: C Phase One Started Phase 0 Complete Phase Zero Ended Phase 1 Complete Phase One Ended Thread: C Phase Two Started Thread: A Phase Two Started Thread: B Phase Two Started Phase 2 Complete Phase Two Ended Phaser is terminated
示例2:
Java
// Java program to show Phaser Class
import java.util.concurrent.Phaser;
// A thread of execution that uses a phaser.
class MyThread implements Runnable {
Phaser phaser;
String title;
public MyThread(Phaser phaser, String title)
{
this.phaser = phaser;
this.title = title;
phaser.register();
new Thread(this).start();
}
@Override public void run()
{
System.out.println("Thread: " + title
+ " Phase Zero Started");
phaser.arriveAndAwaitAdvance();
// Stop execution to prevent jumbled output
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Thread: " + title
+ " Phase One Started");
phaser.arriveAndAwaitAdvance();
// Stop execution to prevent jumbled output
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Thread: " + title
+ " Phase Two Started");
phaser.arriveAndDeregister();
}
}
public class PhaserExample {
public static void main(String[] args)
{
Phaser phaser = new Phaser();
phaser.register();
int currentPhase;
System.out.println("Starting");
new MyThread(phaser, "A");
new MyThread(phaser, "B");
new MyThread(phaser, "C");
// Wait for all threads to complete phase Zero.
currentPhase = phaser.getPhase();
phaser.arriveAndAwaitAdvance();
System.out.println("Phase " + currentPhase
+ " Complete");
System.out.println("Phase Zero Ended");
// Wait for all threads to complete phase One.
currentPhase = phaser.getPhase();
phaser.arriveAndAwaitAdvance();
System.out.println("Phase " + currentPhase
+ " Complete");
System.out.println("Phase One Ended");
currentPhase = phaser.getPhase();
phaser.arriveAndAwaitAdvance();
System.out.println("Phase " + currentPhase
+ " Complete");
System.out.println("Phase Two Ended");
// Deregister the main thread.
phaser.arriveAndDeregister();
if (phaser.isTerminated()) {
System.out.println("Phaser is terminated");
}
}
}
输出
Starting Thread: C Phase Zero Started Thread: A Phase Zero Started Thread: B Phase Zero Started Thread: B Phase One Started Thread: C Phase One Started Thread: A Phase One Started Phase 0 Complete Phase Zero Ended Phase 1 Complete Phase One Ended Thread: A Phase Two Started Thread: C Phase Two Started Thread: B Phase Two Started Phase 2 Complete Phase Two Ended Phaser is terminated
方法:
- int register()- 此方法用于在构建移相器后注册各方。它返回其注册的阶段的阶段编号。
public int register() throws IllegalArgumentException
- int arrive()- 此方法表示线程已完成任务的某些部分。它不会暂停调用线程的执行。如果移相器已终止,它将返回当前阶段编号或负值。
public int arrive() throws IllegalStateException
- int arriveAndDeregister()- 此方法使线程能够到达某个阶段并自行注销,而无需等待其他线程到达。如果移相器已终止,它将返回当前阶段编号或负值。
public int arriveAndDeregister() throws IllegalStateException
- int arriveAndAwaitAdvance()- 该方法在某个阶段暂停线程的执行,以等待其他线程。如果移相器已终止,它将返回当前阶段编号或负值。
public int arriveAndAwaitAdvance() throws IllegalStateException
- 最终int getPhase()- 此方法返回当前阶段号。如果调用移相器终止,则返回负值。
public final int getPhase()
- boolean onAdvance(int 阶段,int 各方)- 此方法有助于定义阶段推进应如何发生。为此,用户必须重写此方法。要终止移相器,onAdvance()方法返回true,否则返回false;
protected boolean onAdvance(int phase, int parties)
演示 Phaser 类方法的示例- 该方法被重写,以便移相器仅执行指定数量的阶段。
Java
// Java program to demonstrate
// the methods of Phaser class
import java.util.concurrent.Phaser;
// Extend MyPhaser and override onAdvance()
// so that only specific number of phases
// are executed
class MyPhaser extends Phaser {
int numPhases;
MyPhaser(int parties, int phaseCount)
{
super(parties);
numPhases = phaseCount - 1;
}
@Override
protected boolean onAdvance(int phase,
int registeredParties)
{
System.out.println("Phase " + phase
+ " completed.\n");
// If all phases have completed, return true.
if (phase == numPhases || registeredParties == 0) {
return true;
}
// otherwise, return false
return false;
}
}
// A thread of execution that uses a phaser
class ModifiedThread implements Runnable {
Phaser phsr;
String name;
ModifiedThread(Phaser p, String n)
{
phsr = p;
name = n;
phsr.register();
new Thread(this).start();
}
@Override public void run()
{
while (!phsr.isTerminated()) {
System.out.println("Thread " + name
+ " Beginning Phase "
+ phsr.getPhase());
phsr.arriveAndAwaitAdvance();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class PhaserExample2 {
public static void main(String[] args)
{
MyPhaser phsr = new MyPhaser(1, 4);
System.out.println("Starting");
new ModifiedThread(phsr, "A");
new ModifiedThread(phsr, "B");
new ModifiedThread(phsr, "C");
while (!phsr.isTerminated()) {
phsr.arriveAndAwaitAdvance();
}
System.out.println("The phaser is terminated\n");
}
}
输出
Starting Thread B Beginning Phase 0 Thread C Beginning Phase 0 Thread A Beginning Phase 0 Phase 0 completed. Thread A Beginning Phase 1 Thread B Beginning Phase 1 Thread C Beginning Phase 1 Phase 1 completed. Thread C Beginning Phase 2 Thread A Beginning Phase 2 Thread B Beginning Phase 2 Phase 2 completed. Thread A Beginning Phase 3 Thread B Beginning Phase 3 Thread C Beginning Phase 3 Phase 3 completed. The phaser is terminated
参考: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Phaser.html
相关用法
- Java Java.util.concurrent.RecursiveAction用法及代码示例
- Java Java.util.concurrent.RecursiveTask用法及代码示例
- Java Java.util.concurrent.CyclicBarrier用法及代码示例
- Java Java.util.ArrayDeque.add()用法及代码示例
- Java Java.util.ArrayDeque.addFirst()用法及代码示例
- Java Java.util.ArrayDeque.addLast()用法及代码示例
- Java Java.util.ArrayDeque.clear()用法及代码示例
- Java Java.util.ArrayDeque.clone()用法及代码示例
- Java Java.util.ArrayDeque.descendingIterator()用法及代码示例
- Java Java.util.ArrayDeque.element()用法及代码示例
- Java Java.util.ArrayDeque.getFirst()用法及代码示例
- Java Java.util.ArrayDeque.getLast()用法及代码示例
- Java Java.util.ArrayDeque.isEmpty()用法及代码示例
- Java Java.util.ArrayDeque.iterator()用法及代码示例
- Java Java.util.ArrayDeque.peek()用法及代码示例
- Java Java.util.ArrayDeque.peekFirst()用法及代码示例
- Java Java.util.ArrayDeque.peekLast()用法及代码示例
- Java Java.util.ArrayDeque.poll()用法及代码示例
- Java Java.util.ArrayDeque.pollFirst()用法及代码示例
- Java Java.util.ArrayDeque.pollLast()用法及代码示例
- Java Java.util.ArrayDeque.pop()用法及代码示例
- Java Java.util.ArrayDeque.push()用法及代码示例
- Java Java.util.ArrayDeque.remove()用法及代码示例
- Java Java.util.ArrayDeque.removeFirst()用法及代码示例
- Java Java.util.ArrayDeque.removeLast()用法及代码示例
注:本文由纯净天空筛选整理自CharchitKapoor大神的英文原创作品 Java.util.concurrent.Phaser class in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。