The Wayback Machine - https://web.archive.org/web/20240717024451/https://www.geeksforgeeks.org/java-util-concurrent-phaser-class-in-java-with-examples/
Open In App

Java.util.concurrent.Phaser class in Java with Examples

Last Updated : 19 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Phaser’s primary purpose is to enable synchronization of threads that represent one or more phases of activity. It lets us define a synchronization object that waits until a specific phase has been completed. It then advances to the next phase until that phase concludes. It can also be used to synchronize a single phase, and in that regard, it acts much like a CyclicBarrier.

Class Hierarchy  

java.lang.Object
  ? java.util.concurrent
    ? Class Phaser 

Syntax  

public class Phaser
  extends Object

Constructors: 

  • Phaser() – This creates a phaser with initially zero registered parties. A thread can only use this phaser after registering for it.
public Phaser()
  • Phaser(int parties) – This creates a phaser that requires parties number of threads to advance to the next phase.
public Phaser(int parties)
throws IllegalArgumentException
  • Phaser(Phaser parent) – This specifies a parent phaser for the new object. The number of registered parties is set to zero.
public Phaser(Phaser parent)
  • Phaser(Phaser parent, int parties) – This specifies a parent phaser for the newly created object and the number of parties required to advance to the next phase.
public Phaser(Phaser parent, int parties)
throws IllegalArgumentException

Example1: 

Note: Output may vary with each run.  

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");
        }
    }
}


Output

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

Example2: 

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");
        }
    }
}


Output

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

Methods: 

  • int register() – This method is used to register parties after a phaser has been constructed. It returns the phase number of the phase to which it is registered.
public int register()
throws IllegalArgumentException
  • int arrive() – This method signals that a thread has completed some portion of the task. It does not suspend the execution of the calling thread. It returns the current phase number or a negative value if the phaser has been terminated.
public int arrive()
throws IllegalStateException
  • int arriveAndDeregister() – This method enables a thread to arrive at a phase and deregister itself, without waiting for other threads to arrive. It returns the current phase number or a negative value if the phaser has been terminated.
public int arriveAndDeregister()
throws IllegalStateException
  • int arriveAndAwaitAdvance() – This method suspends the execution of the thread at a phase, to wait for other threads. It returns the current phase number or a negative value if the phaser has been terminated.
public int arriveAndAwaitAdvance()
throws IllegalStateException
  • final int getPhase() – This method returns the current phase number. A negative value is returned if the invoking phasers terminated.
public final int getPhase() 
  • boolean onAdvance(int phase, int parties) – This method helps in defining how a phase advancement should occur. To do this, the user must override this method. To terminate the phaser, onAdvance() method returns true, otherwise, it returns false;
protected boolean onAdvance(int phase, int parties)

Example to demonstrate the methods of Phaser class – where the method is overridden so that the phaser executes only a specified number of phases. 

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");
    }
}


Output

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

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Phaser.html
 



Similar Reads

Java.util.concurrent.RecursiveAction class in Java with Examples
RecursiveAction is an abstract class encapsulates a task that does not return a result. It is a subclass of ForkJoinTask, which is an abstract class representing a task that can be executed on a separate core in a multicore system. The RecursiveAction class is extended to create a task that has a void return type. The code that represents the compu
3 min read
Java.util.concurrent.RecursiveTask class in Java with Examples
RecursiveTask is an abstract class encapsulates a task that returns a result. It is a subclass of ForkJoinTask. The RecursiveTask class is extended to create a task that has a particular return type. The code that represents the computational portion of the task is kept within the compute() method of RecursiveTask. RecursiveTask class is mostly use
2 min read
Java.util.concurrent.Exchanger class with Examples
Exchanger is the most interesting synchronization class of Java. It facilitates the exchange of elements between a pair of threads by creating a synchronization point. It simplifies the exchange of data between two threads. Its operation is simple: it simply waits until two separate threads call its exchange() method. When that occurs, it exchanges
3 min read
Java.util.concurrent.Executor interface with Examples
The concurrent API in Java provides a feature known as an executor that initiates and controls the execution of threads. As such, an executor offers an alternative to managing threads using the thread class. At the core of an executor is the Executor interface. It refers to the objects that execute submitted Runnable tasks. Class hierarchy: java.ut
1 min read
Java.util.concurrent.ExecutorService Interface with Examples
The ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. It is defined in java.util.concurrent package. It defines methods that execute the threads that return results, a set of threads that determine the shutdown status. The ExecutorService interface is implemented in a utility class c
3 min read
Java.util.concurrent.CyclicBarrier in Java
CyclicBarrier is used to make threads wait for each other. It is used when different threads process a part of computation and when all threads have completed the execution, the result needs to be combined in the parent thread. In other words, a CyclicBarrier is used when multiple thread carry out different sub tasks and the output of these sub tas
6 min read
java.util.concurrent Package
Java Concurrency package covers concurrency, multithreading, and parallelism on the Java platform. Concurrency is the ability to run several or multi programs or applications in parallel. The backbone of Java concurrency is threads (a lightweight process, which has its own files and stacks and can access the shared data from other threads in the sa
9 min read
Java.util.BitSet class in Java with Examples | Set 1
BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Constructors: BitSet class Constructors / \ BitSet() BitSet(int no_Of_Bits)BitSet() : A no-argument constructor to create an empty BitSet object. BitSet(int no_Of_Bits): A one-constructor with an integer argument to create an instance of t
2 min read
Java.util.BitSet class methods in Java with Examples | Set 2
Methods discussed in this post: BitSet class methods. / / | | \ \ set() xor() clone() clear() length() cardinality() We strongly recommend to refer below set 1 as a prerequisite of this. BitSet class in Java | Set 1 set() : java.util.BitSet.set() method is a sets the bit at the specified index to the specified value. Syntax:public void set(int bitp
4 min read
Java.util.IntSummaryStatistics class with Examples
The IntSummaryStatistics class is present in java.util package. It takes a collection of Integer objects and is useful in the circumstances when we are dealing with a stream of integers. It maintains a count of the number of integers it has processed, their sum and various other statistics. The class can also be used with Streams. It is useful in t
3 min read
three90RightbarBannerImg