CyclicBarrier

Initalise with 0 and then count up to a defined target. Once it reaches the target, the await() will be release.

CyclicBarrier cyclicBarrier = new CyclicBarrier(2)

await()

Calling await() will:

  1. Block the current thread to wait for CyclicBarrier to reach the target
  2. Count up the cyclic barrier

Example

Given the above CyclicBarrier with target = 2.

From Thread 1:

When calling await() from Thread 1, it will block the current thread and also increase the cyclicBarrier by 1

cyclicBarrier.await() // block, cyclicBarrier == 1
// Start executing ...

From Thread 2:

When calling await() from Thread 2, it will block the current thread and also increase the cyclicBarrier by 1

cyclicBarrier.await() // block, cyclicBarrier == 2
// Now releasing all threads and execute the next code

Now since the cyclicBarrier == 2 we release all threads