JavaMadeSoEasy.com: Implementation of custom/own CyclicBarrier in java - Detailed explanation with full program
There might be situation where we might have to trigger event only when one or more threads completes certain operation.
2 or more threads wait for each other to reach a common barrier point. When all threads have reached common barrier point (i.e. when all threads have called await() method) >
- All waiting threads are released, and
- Event can be triggered as well.
- int await() throws InterruptedException
If the current thread is not the last to arrive(i.e. call await() method) then it waits until one of the following things happens -
- The last thread to call arrive(i,.e. call await() method), or
- Some other thread interrupts the current thread, or
- Some other thread interrupts one of the other waiting threads, or
- Some other thread times out while waiting for barrier, or
- Some other thread invokes reset() method on this cyclicBarrier.
- CyclicBarrier’s constructor >
- CyclicBarrier(int parties)
- CyclicBarrier(int parties, Runnable barrierAction)
- CyclicBarrier’s await() method has 2 forms :
- int await()
- int await(long timeout, TimeUnit unit)
The barrier is called cyclic because CyclicBarrier can be reused after -
- All the waiting threads are released and
- event has been triggered.
class CyclicBarrierCustom{
int initialParties; //total parties
int partiesAwait; //parties yet to arrive
Runnable cyclicBarrrierEvent;
/**
* New CyclicBarrier is created where parties number of thread wait for each
* other to reach common barrier point, when all threads have reached common
* barrier point, parties number of waiting threads are released and
* barrierAction (event) is triggered.
*/
public CyclicBarrierCustom(int parties, Runnable cyclicBarrrierEvent) {
initialParties=parties;
partiesAwait=parties;
this.cyclicBarrrierEvent=cyclicBarrrierEvent;
}
/**
* If the current thread is not the last to arrive(i.e. call await() method) then
it waits until one of the following things happens -
- The last thread to call arrive(i,.e. call await() method), or
- Some other thread interrupts the current thread, or
- Some other thread interrupts one of the other waiting threads, or
- Some other thread times out while waiting for barrier, or
- Some other thread invokes reset() method on this cyclicBarrier.
*/
public synchronized void await() throws InterruptedException {
//decrements awaiting parties by 1.
partiesAwait--;
//If the current thread is not the last to arrive, thread will wait.
if(partiesAwait>0){
this.wait();
}
/*If the current thread is last to arrive, notify all waiting threads, and
launch event*/
else{
/* All parties have arrive, make partiesAwait equal to initialParties,
so that CyclicBarrier could become cyclic. */
partiesAwait=initialParties;
notifyAll(); //notify all waiting threads
cyclicBarrrierEvent.run(); //launch event
}
}
}
Read full article from JavaMadeSoEasy.com: Implementation of custom/own CyclicBarrier in java - Detailed explanation with full program