Understanding CyclicBarrier in java
Barrier in CyclicBarrier implementation between threads stops all thread’s execution till CyclicBarrier count is reached. This can be used to perform mid way calculation. Following code snippet demonstrates CyclicBarrier implementation
Step 1: Create classes which need to halt their execution on calling CyclicBarrier.await()
——————————————————————————————–
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.cyclicbarrier.CyclicBarrierExample;
public class CBClass1 implements Runnable {
private CyclicBarrier barrier;
private String name;
public String getName() {
return name;
}
CBClass1(CyclicBarrier cb, String s) {
this.barrier = cb;
this.name = “****”+s;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + ” is waiting on barrier”);
barrier.await();
System.out.println(Thread.currentThread().getName() + ” has crossed the barrier”);
} catch (InterruptedException | BrokenBarrierException ex) {
Logger.getLogger(CyclicBarrierExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
———————-
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.cyclicbarrier.CyclicBarrierExample;
public class CBClass2 implements Runnable {
private CyclicBarrier barrier;
private String name;
public String getName() {
return name;
}
CBClass2(CyclicBarrier cb, String s) {
this.barrier = cb;
this.name = s+”****”;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + ” is waiting on barrier”);
barrier.await();
System.out.println(Thread.currentThread().getName() + ” has crossed the barrier”);
} catch (InterruptedException | BrokenBarrierException ex) {
Logger.getLogger(CyclicBarrierExample.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Step 2: Create an Executor which use CyclicBarrier to process values during execution
—————————————————————————————-
Note, running this example will not complete Executor execution as CyclicBarrier has not reached yet (in order to complete execution, executor needs to execute another thread which calls CyclicBarrier.await())
import java.util.concurrent.CyclicBarrier;
public class Executor implements Runnable {
final static CyclicBarrier cb = new CyclicBarrier(3);
public static void main(String[] args) {
CBClass1 obj1 = new CBClass1(cb, “Sheetal “);
CBClass2 obj2 = new CBClass2(cb, “Rastogi”);
// starting each of thread
Thread t1 = new Thread(obj1, “Thread 1”);
Thread t2 = new Thread(obj2, “Thread 2”);
System.out.println(obj1.getName() + obj2.getName());
// Thread t3 = new Thread(obj1, “Thread 3″);
t1.start();
t2.start();
// t3.start();
Thread t = new Thread(new Executor());
t.start();
}
@Override
public void run() {
System.out.println(
cb.getParties() – cb.getNumberWaiting() + ” threads is/are waiting at barrier that needs to go…”);
}
}
Output:
——–
****Sheetal Rastogi****
Thread 1 is waiting on barrier
Thread 2 is waiting on barrier
2 threads is/are waiting at barrier that needs to go…
0 comments