Java - Inherit Phaser to implement an action upon a phase advance using its onAdvance() method.

Introduction

The onAdvance() method in the Phaser class is declared as follows.

The first argument is the phase number and the second is the number of registered parties.

protected boolean onAdvance(int phase, int registeredParties)

onAdvance() method can control the termination state of a Phaser.

A Phaser is terminated if its onAdvance() method returns true.

isTerminated() method of the Phaser class returns if a phaser is terminated.

You can terminate a phaser using its forceTermination() method.

The following code demonstrates how to add a Phaser action.

It uses an anonymous class which overrides the onAdvance() method to define a Phaser action to create a custom Phaser class.

It prints a message in the onAdvance() method as the Phaser action.

It returns false, which means the phaser will not be terminated from the onAdvance() method.

Then it registers the self as a subtask and triggers a phase advance using the arriveAndDeregister() method.

Demo

import java.util.concurrent.Phaser;

public class Main {
  public static void main(String[] args) {
    Phaser phaser = new Phaser() {
      protected boolean onAdvance(int phase, int parties) {
        System.out.println("Inside onAdvance(): phase = " + phase
            + ", Registered Parties = " + parties);
        return false;
      }//from w w  w. j  a  va  2s.  c om
    };

    // Register the self (the "main" thread) as a party
    phaser.register();

    // Phaser is not terminated here
    System.out.println("#1: isTerminated():" + phaser.isTerminated());

    // Since we have only one party registered, this arrival will advance
    // the phaser and registered parties reduces to zero
    phaser.arriveAndDeregister();
    /*
    final int TASK_COUNT = 3;
    phaser.bulkRegister(TASK_COUNT);
    for (int i = 1; i <= TASK_COUNT; i++) {
      String taskName = "Task #" + i;
      MyTask task = new MyTask(taskName, phaser);
      task.start();
    }*/
    // Trigger another phase advance
    phaser.register();
    phaser.arriveAndDeregister();

    
    
    // Phaser is still not terminated
    System.out.println("#2: isTerminated():" + phaser.isTerminated());
    // Terminate the phaser
    phaser.forceTermination();

    // Phaser is terminated
    System.out.println("#3: isTerminated():" + phaser.isTerminated());
  }
}

class MyTask extends Thread {
  private Phaser phaser;
  private String taskName;

  public MyTask(String taskName, Phaser phaser) {
    this.taskName = taskName;
    this.phaser = phaser;
  }

  @Override
  public void run() {
    System.out.println(taskName + ":Initializing...");

    int sleepTime = 3;
    try {
      Thread.sleep(sleepTime * 1000);
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println(taskName + ":Initialized...");
    // Wait for all parties to arrive to start the task
    phaser.arriveAndAwaitAdvance();
    System.out.println(taskName + ":Started...");
  }
}

Result

Related Topic