CyclicBarrier Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
- Attributes
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.
Cyclic |
Creates a new |
Cyclic |
Creates a new |
Cyclic |
A constructor used when creating managed representations of JNI objects; called by the runtime. |
Class |
Returns the runtime class of this |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
Is |
Queries if this barrier is in a broken state. |
Jni |
(Inherited from Object) |
Jni |
|
Number |
Returns the number of parties currently waiting at the barrier. |
Parties |
Returns the number of parties required to trip this barrier. |
Peer |
(Inherited from Object) |
Threshold |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
Threshold |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
Await() |
Waits until all #getParties parties have invoked
|
Await(Int64, Time |
Waits until all #getParties parties have invoked
|
Await |
|
Await |
|
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) |
Get |
Returns a hash code value for the object. (Inherited from Object) |
Java |
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) |
Notify |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
Reset() |
Resets the barrier to its initial state. |
Set |
Sets the Handle property. (Inherited from Object) |
To |
(Inherited from Object) |
To |
Returns a string representation of the object. (Inherited from Object) |
Unregister |
(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, 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) |
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) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
IJava |
(Inherited from Object) |
Java |
Performs an Android runtime-checked type conversion. |
Java |
|
Get |