Package pt.ul.jarmus.deadlocks

Source Code of pt.ul.jarmus.deadlocks.IterativeAveraging1D

package pt.ul.jarmus.deadlocks;

import java.util.Arrays;
import java.util.concurrent.Phaser;

import pt.ul.jarmus.JArmus;
/**
* Iterative averaging example.
* @author Tiago Cogumbreiro
*
*/
public class IterativeAveraging1D {
  private static final int I = 20;
  protected static final int J = 100;
  static final int[] a = new int[I + 2];
  public static void main(String[] args) {
    final Phaser c = new Phaser(1); // "Cyclic barrier" phaser
    final Phaser b = new Phaser(1); // "Join barrier" phaser
    JArmus.register(c);
    JArmus.register(b);
    for (int _i = 1; _i <= I; _i++) {
      final int i = _i;
      c.register();
      b.register();
      new Thread() { // Spawn task i
        public void run() {
          JArmus.register(c);
          JArmus.register(b);
          for (int j = 1; j <= J; j++) {
            final int l = a[i - 1];
            final int r = a[i + 1];
            c.arriveAndAwaitAdvance();
            a[i] = (l + r) / 2;
            c.arriveAndAwaitAdvance();
          }
          c.arriveAndDeregister();
          b.arriveAndDeregister();
        }
      }.start();
    }
    b.arriveAndAwaitAdvance();
    handle(a);
  }
  private static void handle(int[] array) {
    System.out.println(Arrays.toString(array));
  }
}
TOP

Related Classes of pt.ul.jarmus.deadlocks.IterativeAveraging1D

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.