Java Phaser control phase change in concurrent phased tasks

Introduction

The Phaser class onAdvance() method is executed each time the phaser changes the phase.

It receives two parameters: the number of the current phase and the number of registered participants.

It returns false if the phaser continues its execution, or true if the phaser has finished and has to enter into the termination state.

import java.util.Date;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

public class Main {

  public static void main(String[] args) {
    MyPhaser phaser = new MyPhaser();

    Language langs[] = new Language[5];
    for (int i = 0; i < langs.length; i++) {
      langs[i] = new Language(phaser);
      phaser.register();/*w w w . ja  va2  s  .  c o m*/
    }

    Thread threads[] = new Thread[langs.length];
    for (int i = 0; i < langs.length; i++) {
      threads[i] = new Thread(langs[i], "Student " + i);
      threads[i].start();
    }

    for (int i = 0; i < threads.length; i++) {
      try {
        threads[i].join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println("Main: The phaser has finished: "+ phaser.isTerminated());

  }
}

class MyPhaser extends Phaser {

  @Override
  protected boolean onAdvance(int phase, int registeredParties) {
    switch (phase) {
    case 0:
      return compile();
    case 1:
      return test();
    case 2:
      return run();
    case 3:
      return release();
    default:
      return true;
    }
  }

  private boolean compile() {
    System.out.println("compile: "+ getRegisteredParties());
    return false;
  }

  private boolean test() {
    System.out.printf("test\n");
    return false;
  }

  private boolean run() {
    System.out.printf("run\n");
    return false;
  }

  private boolean release() {
    System.out.printf("release\n");
    return true;
  }
}

class Language implements Runnable {

  private Phaser phaser;

  public Language(Phaser phaser) {
    this.phaser = phaser;
  }

  public void run() {
    System.out.println(Thread.currentThread().getName()+" "+ new Date());
    phaser.arriveAndAwaitAdvance();
    System.out.println("A:"+" "+ Thread.currentThread().getName()+" " + new Date());
    compile();
    System.out.println("B:"+ Thread.currentThread().getName()+" "+new Date());
    phaser.arriveAndAwaitAdvance();
    System.out.println("C:"+ Thread.currentThread().getName()+" "+new Date());
    test();
    System.out.println("D:"+ Thread.currentThread().getName()+" "+new Date());
    phaser.arriveAndAwaitAdvance();
    System.out.println("E:"+ Thread.currentThread().getName()+" "+new Date());
    release();
    System.out.println("F:"+ Thread.currentThread().getName()+" "+new Date());
    phaser.arriveAndAwaitAdvance();
  }

  private void compile() {
    try {
      TimeUnit.SECONDS.sleep(10);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private void test() {
    try {
      TimeUnit.SECONDS.sleep(13);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private void release() {
    try {
      TimeUnit.SECONDS.sleep(18);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

}



PreviousNext

Related