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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。