Java Java类与示例
Phaser 的主要目的是使代表一个或多个活动阶段的线程能够同步。它允许我们定义一个同步对象,该对象等待特定阶段完成。然后它进入下一阶段,直到该阶段结束。它还可以用于同步单个阶段,在这方面,它的作用很像 CyclicBarrier。
类层次结构
java.lang.Object
? java.util.concurrent
? Class Phaser
句法
public class Phaser
extends Object
构造函数:
- Phaser() - 这将创建一个初始注册方为零的移相器。线程只有在注册后才能使用此移相器。
public Phaser()
- Phaser(intparty) ——这将创建一个相位器,它需要参与方的线程数才能进入下一阶段。
public Phaser(int parties)
throws IllegalArgumentException
- Phaser(Phaser parent) – 这指定了新对象的父移相器。注册方的数量设置为零。
public Phaser(Phaser parent)
- Phaser(Phaser parent, intparty) ——这为新创建的对象指定了一个父移相器,以及进入下一阶段所需的参与方数量。
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");
}
}
}
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");
}
}
}
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 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 reach() - 此方法表示线程已完成任务的某些部分。它不会暂停调用线程的执行。如果移相器已终止,则返回当前相位编号或负值。
public int arrive()
throws IllegalStateException
- int reachAndDeregister() – 此方法使线程能够到达一个阶段并自行取消注册,而无需等待其他线程到达。如果移相器已终止,则返回当前相位编号或负值。
public int arriveAndDeregister()
throws IllegalStateException
- int reachAndAwaitAdvance() - 该方法在一个阶段暂停线程的执行,以等待其他线程。如果移相器已终止,则返回当前相位编号或负值。
public int arriveAndAwaitAdvance()
throws IllegalStateException
- final int getPhase() - 此方法返回当前阶段编号。如果调用移相器终止,则返回负值。
public final int getPhase()
- boolean onAdvance(int phase, intparty) - 此方法有助于定义阶段推进应如何发生。为此,用户必须重写此方法。要终止移相器,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