EDU.oswego.cs.dl.util.concurrent

Class CyclicBarrier

public class CyclicBarrier extends Object implements Barrier

A cyclic barrier is a reasonable choice for a barrier in contexts involving a fixed sized group of threads that must occasionally wait for each other. (A Rendezvous better handles applications in which any number of threads meet, n-at-a-time.)

CyclicBarriers use an all-or-none breakage model for failed synchronization attempts: If threads leave a barrier point prematurely because of timeout or interruption, others will also leave abnormally (via BrokenBarrierException), until the barrier is restarted. This is usually the simplest and best strategy for sharing knowledge about failures among cooperating threads in the most common usages contexts of Barriers. This implementation has the property that interruptions among newly arriving threads can cause as-yet-unresumed threads from a previous barrier cycle to return out as broken. This transmits breakage as early as possible, but with the possible byproduct that only some threads returning out of a barrier will realize that it is newly broken. (Others will not realize this until a future cycle.) (The Rendezvous class has a more uniform, but sometimes less desirable policy.)

Barriers support an optional Runnable command that is run once per barrier point.

Sample usage Here is a code sketch of a barrier in a parallel decomposition design.

 class Solver {
   final int N;
   final float[][] data;
   final CyclicBarrier barrier;
   
   class Worker implements Runnable {
      int myRow;
      Worker(int row) { myRow = row; }
      public void run() {
         while (!done()) {
            processRow(myRow);

            try {
              barrier.barrier(); 
            }
            catch (InterruptedException ex) { return; }
            catch (BrokenBarrierException ex) { return; }
         }
      }
   }

   public Solver(float[][] matrix) {
     data = matrix;
     N = matrix.length;
     barrier = new CyclicBarrier(N);
     barrier.setBarrierCommand(new Runnable() {
       public void run() { mergeRows(...); }
     });
     for (int i = 0; i < N; ++i) {
       new Thread(new Worker(i)).start();
     waitUntilDone();
    }
 }
 

[ Introduction to this package. ]

Field Summary
protected RunnablebarrierCommand_
protected booleanbroken_
protected intcount_
protected intparties_
protected intresets_
Constructor Summary
CyclicBarrier(int parties)
Create a CyclicBarrier for the indicated number of parties, and no command to run at each barrier.
CyclicBarrier(int parties, Runnable command)
Create a CyclicBarrier for the indicated number of parties. and the given command to run at each barrier point.
Method Summary
intattemptBarrier(long msecs)
Enter barrier and wait at most msecs for the other parties()-1 threads.
intbarrier()
Enter barrier and wait for the other parties()-1 threads.
booleanbroken()
protected intdoBarrier(boolean timed, long msecs)
intparties()
voidrestart()
Reset to initial state.
RunnablesetBarrierCommand(Runnable command)
Set the command to run at the point at which all threads reach the barrier.

Field Detail

barrierCommand_

protected Runnable barrierCommand_

broken_

protected boolean broken_

count_

protected int count_

parties_

protected final int parties_

resets_

protected int resets_

Constructor Detail

CyclicBarrier

public CyclicBarrier(int parties)
Create a CyclicBarrier for the indicated number of parties, and no command to run at each barrier.

Throws: IllegalArgumentException if parties less than or equal to zero.

CyclicBarrier

public CyclicBarrier(int parties, Runnable command)
Create a CyclicBarrier for the indicated number of parties. and the given command to run at each barrier point.

Throws: IllegalArgumentException if parties less than or equal to zero.

Method Detail

attemptBarrier

public int attemptBarrier(long msecs)
Enter barrier and wait at most msecs for the other parties()-1 threads.

Returns: if not timed out, the arrival index: the number of other parties that were still waiting upon entry. This is a unique value from zero to parties()-1. If it is zero, then the current thread was the last party to hit barrier point and so was responsible for releasing the others.

Throws: BrokenBarrierException if any other thread in any previous or current barrier since either creation or the last restart operation left the barrier prematurely due to interruption or time-out. (If so, the broken status is also set.) Threads that are noticed to have been interrupted after being released are not considered to have broken the barrier. In all cases, the interruption status of the current thread is preserved, so can be tested by checking Thread.interrupted. InterruptedException if this thread was interrupted during the barrier. If so, broken status is also set. TimeoutException if this thread timed out waiting for the barrier. If the timeout occured while already in the barrier, broken status is also set.

barrier

public int barrier()
Enter barrier and wait for the other parties()-1 threads.

Returns: the arrival index: the number of other parties that were still waiting upon entry. This is a unique value from zero to parties()-1. If it is zero, then the current thread was the last party to hit barrier point and so was responsible for releasing the others.

Throws: BrokenBarrierException if any other thread in any previous or current barrier since either creation or the last restart operation left the barrier prematurely due to interruption or time-out. (If so, the broken status is also set.) Threads that are notified to have been interrupted after being released are not considered to have broken the barrier. In all cases, the interruption status of the current thread is preserved, so can be tested by checking Thread.interrupted. InterruptedException if this thread was interrupted during the barrier, and was the one causing breakage. If so, broken status is also set.

broken

public boolean broken()

doBarrier

protected int doBarrier(boolean timed, long msecs)

parties

public int parties()

restart

public void restart()
Reset to initial state. Clears both the broken status and any record of waiting threads, and releases all currently waiting threads with indeterminate return status. This method is intended only for use in recovery actions in which it is somehow known that no thread could possibly be relying on the the synchronization properties of this barrier.

setBarrierCommand

public Runnable setBarrierCommand(Runnable command)
Set the command to run at the point at which all threads reach the barrier. This command is run exactly once, by the thread that trips the barrier. The command is not run if the barrier is broken.

Parameters: command the command to run. If null, no command is run.

Returns: the previous command