CyclicBarrier
CyclicBarrier class defines a synchronization object that suspends until the specified number of threads has reached the barrier point.
CyclicBarrier has the following two constructors:
CyclicBarrier(int numThreads, Runnable action)
CyclicBarrier(int numThreads)
- numThreads specifies the number of threads that must reach the barrier before execution continues. action specifies a thread that will be executed when the barrier is reached.
The general procedure to use CyclicBarrier.
- create a CyclicBarrier object
- specifying the number of threads that you will be waiting for.
- when each thread reaches the barrier, have it call await( ) on that object.
- This will pause execution of the thread until all of the other threads also call await( ).
Once the specified number of threads has reached the barrier, await( ) will return, and execution will resume. If you have specified an action, then that thread is executed.
The await( ) method has the following two forms:
int await( ) throws InterruptedException, BrokenBarrierException
- waits until the all threads have reached the barrier point.
int await(long wait, TimeUnit tu) throws InterruptedException, BrokenBarrierException, TimeoutException
- waits only for the period of time specified by wait. The units represented by wait are specified by tu.
Both forms return a value that indicates the order that the threads arrive at the barrier point. The first thread returns a value equal to the number of threads waited upon minus one. The last thread returns zero.
public class Main {
public static void main(String args[]) {
CyclicBarrier cb = new CyclicBarrier(3, new BarAction());
System.out.println("Starting");
new MyThread(cb, "A");
new MyThread(cb, "B");
new MyThread(cb, "C");
}
}
// A thread of execution that uses a CyclicBarrier.
class MyThread extends Thread {
CyclicBarrier cbar;
String name;
MyThread(CyclicBarrier c, String n) {
cbar = c;
name = n;
start();
}
public void run() {
System.out.println(name);
try {
Thread.sleep(3000);
cbar.await();
} catch (Exception exc) {
System.out.println(exc);
}
}
}
// An object of this class is called when the CyclicBarrier ends.
class BarAction implements Runnable {
public void run() {
System.out.println("Barrier Reached!");
}
}
A CyclicBarrier can be reused because it will release waiting threads each time the specified number of threads calls await( ).
import java.util.concurrent.CyclicBarrier;
public class Main {
public static void main(String args[]) {
CyclicBarrier cb = new CyclicBarrier(3, new BarAction());
System.out.println("Starting");
new MyThread(cb, "A");
new MyThread(cb, "B");
new MyThread(cb, "C");
new MyThread(cb, "X");
new MyThread(cb, "Y");
new MyThread(cb, "Z");
}
}
// A thread of execution that uses a CyclicBarrier.
class MyThread extends Thread {
CyclicBarrier cbar;
String name;
MyThread(CyclicBarrier c, String n) {
cbar = c;
name = n;
start();
}
public void run() {
System.out.println(name);
try {
Thread.sleep(3000);
cbar.await();
} catch (Exception exc) {
System.out.println(exc);
}
}
}
// An object of this class is called when the CyclicBarrier ends.
class BarAction implements Runnable {
public void run() {
System.out.println("Barrier Reached!");
}
}