Example usage for java.util.concurrent Phaser Phaser

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

Introduction

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

Prototype

public Phaser() 

Source Link

Document

Creates a new phaser with no initially registered parties, no parent, and initial phase number 0.

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;
        }//www .  java 2s  .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 w w. j a  v a2 s.com*/
                    "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);
}

From source file:Main.java

private static Long fib(int n) throws InterruptedException {
    AtomicLong result = new AtomicLong();
    Phaser phaser = new Phaser();
    Task initialTask = new Task(n, result, phaser);
    phaser.register();//w ww  .  j a  va 2  s . com
    executors.submit(initialTask);
    phaser.arriveAndAwaitAdvance();
    return result.get();
}

From source file:nextflow.fs.dx.DxUploadOutputStream.java

/**
 * Initialize the uploader output stream for the specified file
 *
 * @param fileId The target DnaNexus file
 * @param remote The DnaNexus API wrapper object
 * @param maxForks Maximum number of parallel upload jobs allowed (default: 5)
 *//*from   ww  w  .ja  v  a  2  s . c o m*/
public DxUploadOutputStream(String fileId, DxApi remote, int maxForks) {

    this.fileId = fileId;
    this.queue = new ArrayBlockingQueue<>(maxForks);
    this.phaser = new Phaser();
    this.remote = remote;
    this.buf = allocate();
    this.executor = Executors.newCachedThreadPool();
    checkCapacity();
    start();
}