CyclicBarrier Class

Definition

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.

[Android.Runtime.Register("java/util/concurrent/CyclicBarrier", DoNotGenerateAcw=true)]
public class CyclicBarrier : Java.Lang.Object
[<Android.Runtime.Register("java/util/concurrent/CyclicBarrier", DoNotGenerateAcw=true)>]
type CyclicBarrier = class
    inherit Object
Inheritance
CyclicBarrier
Attributes

Remarks

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called <em>cyclic</em> because it can be re-used after the waiting threads are released.

A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. This <em>barrier action</em> is useful for updating shared-state before any of the parties continue.

<b>Sample usage:</b> Here is an example of using a barrier in a parallel decomposition design:

{@code
            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.await();
                    } catch (InterruptedException ex) {
                      return;
                    } catch (BrokenBarrierException ex) {
                      return;
                    }
                  }
                }
              }

              public Solver(float[][] matrix) {
                data = matrix;
                N = matrix.length;
                Runnable barrierAction = () -> mergeRows(...);
                barrier = new CyclicBarrier(N, barrierAction);

                List<Thread> threads = new ArrayList<>(N);
                for (int i = 0; i < N; i++) {
                  Thread thread = new Thread(new Worker(i));
                  threads.add(thread);
                  thread.start();
                }

                // wait until done
                for (Thread thread : threads)
                  try {
                    thread.join();
                  } catch (InterruptedException ex) { }
              }
            }}

Here, each worker thread processes a row of the matrix, then waits at the barrier until all rows have been processed. When all rows are processed the supplied Runnable barrier action is executed and merges the rows. If the merger determines that a solution has been found then done() will return true and each worker will terminate.

If the barrier action does not rely on the parties being suspended when it is executed, then any of the threads in the party could execute that action when it is released. To facilitate this, each invocation of #await returns the arrival index of that thread at the barrier. You can then choose which thread should execute the barrier action, for example:

{@code
            if (barrier.await() == 0) {
              // log the completion of this iteration
            }}

The CyclicBarrier uses an all-or-none breakage model for failed synchronization attempts: If a thread leaves a barrier point prematurely because of interruption, failure, or timeout, all other threads waiting at that barrier point will also leave abnormally via BrokenBarrierException (or InterruptedException if they too were interrupted at about the same time).

Memory consistency effects: Actions in a thread prior to calling await()<i>happen-before</i> actions that are part of the barrier action, which in turn happen-before actions following a successful return from the corresponding await() in other threads.

Added in 1.5.

Java documentation for java.util.concurrent.CyclicBarrier.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

CyclicBarrier(Int32)

Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and does not perform a predefined action when the barrier is tripped.

CyclicBarrier(Int32, IRunnable)

Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and which will execute the given barrier action when the barrier is tripped, performed by the last thread entering the barrier.

CyclicBarrier(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsBroken

Queries if this barrier is in a broken state.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
NumberWaiting

Returns the number of parties currently waiting at the barrier.

Parties

Returns the number of parties required to trip this barrier.

PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Await()

Waits until all #getParties parties have invoked await on this barrier.

Await(Int64, TimeUnit)

Waits until all #getParties parties have invoked await on this barrier, or the specified waiting time elapses.

AwaitAsync()
AwaitAsync(Int64, TimeUnit)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Reset()

Resets the barrier to its initial state.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to