< prev index next >

src/java.base/share/classes/java/util/concurrent/CyclicBarrier.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2020-12
Reviewed-by: martin


  77  *         }
  78  *       }
  79  *     }
  80  *   }
  81  *
  82  *   public Solver(float[][] matrix) {
  83  *     data = matrix;
  84  *     N = matrix.length;
  85  *     Runnable barrierAction = () -> mergeRows(...);
  86  *     barrier = new CyclicBarrier(N, barrierAction);
  87  *
  88  *     List<Thread> threads = new ArrayList<>(N);
  89  *     for (int i = 0; i < N; i++) {
  90  *       Thread thread = new Thread(new Worker(i));
  91  *       threads.add(thread);
  92  *       thread.start();
  93  *     }
  94  *
  95  *     // wait until done
  96  *     for (Thread thread : threads)

  97  *       thread.join();

  98  *   }
  99  * }}</pre>
 100  *
 101  * Here, each worker thread processes a row of the matrix, then waits at the
 102  * barrier until all rows have been processed. When all rows are processed the
 103  * supplied {@link Runnable} barrier action is executed and merges the rows.
 104  * If the merger determines that a solution has been found then {@code done()}
 105  * will return {@code true} and each worker will terminate.
 106  *
 107  * <p>If the barrier action does not rely on the parties being suspended when
 108  * it is executed, then any of the threads in the party could execute that
 109  * action when it is released. To facilitate this, each invocation of
 110  * {@link #await} returns the arrival index of that thread at the barrier.
 111  * You can then choose which thread should execute the barrier action, for
 112  * example:
 113  * <pre> {@code
 114  * if (barrier.await() == 0) {
 115  *   // log the completion of this iteration
 116  * }}</pre>
 117  *




  77  *         }
  78  *       }
  79  *     }
  80  *   }
  81  *
  82  *   public Solver(float[][] matrix) {
  83  *     data = matrix;
  84  *     N = matrix.length;
  85  *     Runnable barrierAction = () -> mergeRows(...);
  86  *     barrier = new CyclicBarrier(N, barrierAction);
  87  *
  88  *     List<Thread> threads = new ArrayList<>(N);
  89  *     for (int i = 0; i < N; i++) {
  90  *       Thread thread = new Thread(new Worker(i));
  91  *       threads.add(thread);
  92  *       thread.start();
  93  *     }
  94  *
  95  *     // wait until done
  96  *     for (Thread thread : threads)
  97  *       try {
  98  *         thread.join();
  99  *       } catch (InterruptedException ex) { }
 100  *   }
 101  * }}</pre>
 102  *
 103  * Here, each worker thread processes a row of the matrix, then waits at the
 104  * barrier until all rows have been processed. When all rows are processed the
 105  * supplied {@link Runnable} barrier action is executed and merges the rows.
 106  * If the merger determines that a solution has been found then {@code done()}
 107  * will return {@code true} and each worker will terminate.
 108  *
 109  * <p>If the barrier action does not rely on the parties being suspended when
 110  * it is executed, then any of the threads in the party could execute that
 111  * action when it is released. To facilitate this, each invocation of
 112  * {@link #await} returns the arrival index of that thread at the barrier.
 113  * You can then choose which thread should execute the barrier action, for
 114  * example:
 115  * <pre> {@code
 116  * if (barrier.await() == 0) {
 117  *   // log the completion of this iteration
 118  * }}</pre>
 119  *


< prev index next >