Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#pubctors) \| [Methods](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#inhmethods)

# CyclicBarrier


`
public


class
CyclicBarrier
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.util.concurrent.CyclicBarrier |

<br />

*** ** * ** ***

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
*cyclic* because it can be re-used after the waiting threads
are released.

A `CyclicBarrier` supports an optional `https://developer.android.com/reference/java/lang/Runnable` command
that is run once per barrier point, after the last thread in the party
arrives, but before any threads are released.
This *barrier action* is useful
for updating shared-state before any of the parties continue.

**Sample usage:** Here is an example of using 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.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 `https://developer.android.com/reference/java/lang/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
`https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#await()` returns the arrival index of that thread at the barrier.
You can then choose which thread should execute the barrier action, for
example:

    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 `https://developer.android.com/reference/java/util/concurrent/BrokenBarrierException` (or
`https://developer.android.com/reference/java/lang/InterruptedException` if they too were interrupted at about
the same time).

Memory consistency effects: Actions in a thread prior to calling
`await()`
[*happen-before*](https://developer.android.com/reference/java/util/concurrent/package-summary#MemoryVisibility)
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.
**See also:**

- `https://developer.android.com/reference/java/util/concurrent/CountDownLatch`
- `https://developer.android.com/reference/java/util/concurrent/Phaser`

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#CyclicBarrier(int)(int parties) ` 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. |
| ` https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#CyclicBarrier(int,%20java.lang.Runnable)(int parties, https://developer.android.com/reference/java/lang/Runnable barrierAction) ` 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. |

| ### Public methods ||
|---|---|
| ` int` | ` https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#await()() ` Waits until all [parties](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#getParties()) have invoked `await` on this barrier. |
| ` int` | ` https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#await(long,%20java.util.concurrent.TimeUnit)(long timeout, https://developer.android.com/reference/java/util/concurrent/TimeUnit unit) ` Waits until all [parties](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#getParties()) have invoked `await` on this barrier, or the specified waiting time elapses. |
| ` int` | ` https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#getNumberWaiting()() ` Returns the number of parties currently waiting at the barrier. |
| ` int` | ` https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#getParties()() ` Returns the number of parties required to trip this barrier. |
| ` boolean` | ` https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#isBroken()() ` Queries if this barrier is in a broken state. |
| ` void` | ` https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#reset()() ` Resets the barrier to its initial state. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### CyclicBarrier

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public CyclicBarrier (int parties)
```

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.

<br />

| Parameters ||
|---|---|
| `parties` | `int`: the number of threads that must invoke `https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#await()` before the barrier is tripped <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `parties` is less than 1 |

### CyclicBarrier

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public CyclicBarrier (int parties, 
                Runnable barrierAction)
```

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.

<br />

| Parameters ||
|---|---|
| `parties` | `int`: the number of threads that must invoke `https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#await()` before the barrier is tripped <br /> |
| `barrierAction` | `Runnable`: the command to execute when the barrier is tripped, or `null` if there is no action <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `parties` is less than 1 |

## Public methods

### await

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int await ()
```

Waits until all [parties](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#getParties()) have invoked
`await` on this barrier.

If the current thread is not the last to arrive then it is
disabled for thread scheduling purposes and lies dormant until
one of the following things happens:

- The last thread arrives; or
- Some other thread [interrupts](https://developer.android.com/reference/java/lang/Thread#interrupt()) the current thread; or
- Some other thread [interrupts](https://developer.android.com/reference/java/lang/Thread#interrupt()) one of the other waiting threads; or
- Some other thread times out while waiting for barrier; or
- Some other thread invokes `https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#reset()` on this barrier.

If the current thread:

- has its interrupted status set on entry to this method; or
- is [interrupted](https://developer.android.com/reference/java/lang/Thread#interrupt()) while waiting

then `https://developer.android.com/reference/java/lang/InterruptedException` is thrown and the current thread's interrupted status is cleared.

If the barrier is `https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#reset()` while any thread is waiting,
or if the barrier [is broken](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#isBroken()) when
`await` is invoked, or while any thread is waiting, then
`https://developer.android.com/reference/java/util/concurrent/BrokenBarrierException` is thrown.

If any thread is [interrupted](https://developer.android.com/reference/java/lang/Thread#interrupt()) while waiting,
then all other waiting threads will throw
`https://developer.android.com/reference/java/util/concurrent/BrokenBarrierException` and the barrier is placed in the broken
state.

If the current thread is the last thread to arrive, and a
non-null barrier action was supplied in the constructor, then the
current thread runs the action before allowing the other threads to
continue.
If an exception occurs during the barrier action then that exception
will be propagated in the current thread and the barrier is placed in
the broken state.

<br />

| Returns ||
|---|---|
| `int` | the arrival index of the current thread, where index `getParties() - 1` indicates the first to arrive and zero indicates the last to arrive <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException` | if the current thread was interrupted while waiting |
| `https://developer.android.com/reference/java/util/concurrent/BrokenBarrierException` | if *another* thread was interrupted or timed out while the current thread was waiting, or the barrier was reset, or the barrier was broken when `await` was called, or the barrier action (if present) failed due to an exception |

### await

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int await (long timeout, 
                TimeUnit unit)
```

Waits until all [parties](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#getParties()) have invoked
`await` on this barrier, or the specified waiting time elapses.

If the current thread is not the last to arrive then it is
disabled for thread scheduling purposes and lies dormant until
one of the following things happens:

- The last thread arrives; or
- The specified timeout elapses; or
- Some other thread [interrupts](https://developer.android.com/reference/java/lang/Thread#interrupt()) the current thread; or
- Some other thread [interrupts](https://developer.android.com/reference/java/lang/Thread#interrupt()) one of the other waiting threads; or
- Some other thread times out while waiting for barrier; or
- Some other thread invokes `https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#reset()` on this barrier.

If the current thread:

- has its interrupted status set on entry to this method; or
- is [interrupted](https://developer.android.com/reference/java/lang/Thread#interrupt()) while waiting

then `https://developer.android.com/reference/java/lang/InterruptedException` is thrown and the current thread's interrupted status is cleared.

If the specified waiting time elapses then `https://developer.android.com/reference/java/util/concurrent/TimeoutException`
is thrown. If the time is less than or equal to zero, the
method will not wait at all.

If the barrier is `https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#reset()` while any thread is waiting,
or if the barrier [is broken](https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#isBroken()) when
`await` is invoked, or while any thread is waiting, then
`https://developer.android.com/reference/java/util/concurrent/BrokenBarrierException` is thrown.

If any thread is [interrupted](https://developer.android.com/reference/java/lang/Thread#interrupt()) while
waiting, then all other waiting threads will throw `https://developer.android.com/reference/java/util/concurrent/BrokenBarrierException` and the barrier is placed in the broken
state.

If the current thread is the last thread to arrive, and a
non-null barrier action was supplied in the constructor, then the
current thread runs the action before allowing the other threads to
continue.
If an exception occurs during the barrier action then that exception
will be propagated in the current thread and the barrier is placed in
the broken state.

<br />

| Parameters ||
|---|---|
| `timeout` | `long`: the time to wait for the barrier <br /> |
| `unit` | `TimeUnit`: the time unit of the timeout parameter <br /> |

| Returns ||
|---|---|
| `int` | the arrival index of the current thread, where index `getParties() - 1` indicates the first to arrive and zero indicates the last to arrive <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException` | if the current thread was interrupted while waiting |
| `https://developer.android.com/reference/java/util/concurrent/BrokenBarrierException` | if *another* thread was interrupted or timed out while the current thread was waiting, or the barrier was reset, or the barrier was broken when `await` was called, or the barrier action (if present) failed due to an exception |
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException` | if the specified timeout elapses. In this case the barrier will be broken. |

### getNumberWaiting

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getNumberWaiting ()
```

Returns the number of parties currently waiting at the barrier.
This method is primarily useful for debugging and assertions.

<br />

| Returns ||
|---|---|
| `int` | the number of parties currently blocked in `https://developer.android.com/reference/java/util/concurrent/CyclicBarrier#await()` <br /> |

### getParties

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getParties ()
```

Returns the number of parties required to trip this barrier.

<br />

| Returns ||
|---|---|
| `int` | the number of parties required to trip this barrier <br /> |

### isBroken

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isBroken ()
```

Queries if this barrier is in a broken state.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if one or more parties broke out of this barrier due to interruption or timeout since construction or the last reset, or a barrier action failed due to an exception; `false` otherwise. <br /> |

### reset

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void reset ()
```

Resets the barrier to its initial state. If any parties are
currently waiting at the barrier, they will return with a
`https://developer.android.com/reference/java/util/concurrent/BrokenBarrierException`. Note that resets *after*
a breakage has occurred for other reasons can be complicated to
carry out; threads need to re-synchronize in some other way,
and choose one to perform the reset. It may be preferable to
instead create a new barrier for subsequent use.

<br />