Example usage for java.util.concurrent Phaser isTerminated

List of usage examples for java.util.concurrent Phaser isTerminated

Introduction

In this page you can find the example usage for java.util.concurrent Phaser isTerminated.

Prototype

public boolean isTerminated() 

Source Link

Document

Returns true if this phaser has been terminated.

Usage

From source file:Main.java

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);
            // Do not terminate the phaser by returning false
            return false;
        }/*from  w  w w  .ja v a2  s  . c o m*/
    };
    // Register the self (the "main" thread) as a party 
    phaser.register();
    System.out.println("#1: isTerminated():" + phaser.isTerminated());
    phaser.arriveAndDeregister();

    // Trigger another phase advance
    phaser.register();
    phaser.arriveAndDeregister();

    System.out.println("#2: isTerminated():" + phaser.isTerminated());
    phaser.forceTermination();
    System.out.println("#3: isTerminated():" + phaser.isTerminated());
}

From source file:AdderTask.java

public static void main(String[] args) {
    final int PHASE_COUNT = 2;
    Phaser phaser = new Phaser() {
        public boolean onAdvance(int phase, int parties) {
            System.out.println(/*from   w  ww .ja va  2s  .c o  m*/
                    "Phase:" + phase + ", Parties:" + parties + ",  Arrived:" + this.getArrivedParties());
            boolean terminatePhaser = false;
            if (phase >= PHASE_COUNT - 1 || parties == 0) {
                terminatePhaser = true;
            }

            return terminatePhaser;
        }
    };
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    int ADDER_COUNT = 3;
    phaser.bulkRegister(ADDER_COUNT + 1);
    for (int i = 1; i <= ADDER_COUNT; i++) {
        String taskName = "Task  #" + i;
        AdderTask task = new AdderTask(taskName, phaser, list);
        task.start();
    }
    while (!phaser.isTerminated()) {
        phaser.arriveAndAwaitAdvance();
    }
    int sum = 0;
    for (Integer num : list) {
        sum = sum + num;
    }
    System.out.println("Sum = " + sum);
}