public class ThreadBarrier extends CyclicBarrier
ThreadBarrier adds a
cause to
BrokenBarrierException thrown by a
CyclicBarrier.reset() operation defined by
CyclicBarrier.
Sample usage:
class MyTestClass implements RemoteEventListener
{
final ThreadBarrier barrier;
class Worker implements Runnable
{
public void run()
{
barrier.await(); //wait for all threads to reach run
try
{
prepare();
barrier.await(); //wait for all threads to prepare
process();
barrier.await(); //wait for all threads to process
}
catch(Throwable t){
log("Worker thread caught exception", t);
barrier.reset(t);
}
}
}
public void testThreads() {
barrier = new ThreadBarrier(N_THREADS + 1);
for (int i = 0; i < N; ++i)
new Thread(new Worker()).start();
try{
barrier.await(); //wait for all threads to reach run
barrier.await(); //wait for all threads to prepare
barrier.await(); //wait for all threads to process
}
catch(BrokenBarrierException bbe) {
Assert.fail(bbe);
}
}
int actualNotificationCount = 0;
public synchronized void notify (RemoteEvent event) {
try{
actualNotificationCount++;
if (actualNotificationCount == EXPECTED_COUNT)
barrier.await(); //signal when all notifications arrive
// too many notifications?
Assert.assertFalse("Exceeded notification count",
actualNotificationCount > EXPECTED_COUNT);
}
catch(Throwable t) {
log("Worker thread caught exception", t);
barrier.reset(t);
}
}
public void testNotify() {
barrier = new ThreadBarrier(N_LISTENERS + 1);
registerNotification();
triggerNotifications();
//wait until either all notifications arrive, or
//until a MAX_TIMEOUT is reached.
barrier.await(MAX_TIMEOUT);
//check if all notifications were accounted for or timed-out
Assert.assertEquals("Notification count",
EXPECTED_COUNT, actualNotificationCount);
//inspect that the barrier isn't broken
barrier.inspect(); //throws BrokenBarrierException if broken
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
ThreadBarrier
A Barrier action to be used in conjunction with
ThreadBarrier to measure performance between barrier awaits.
|
| Constructor and Description |
|---|
ThreadBarrier(int parties)
|
ThreadBarrier(int parties, Runnable
|
| Modifier and Type | Method and Description |
|---|---|
int |
await()
|
int |
await(long timeout, TimeUnit
|
void |
inspect()
Inspects if the barrier is broken.
|
boolean |
isBroken()
Queries if this barrier is in a broken state.
|
void |
reset(Throwable
Resets the barrier to its initial state.
|
getNumberWaiting, getParties, resetpublic ThreadBarrier(int parties)
public ThreadBarrier(int parties,
Runnable barrierAction) public int await()
throws InterruptedException,
BrokenBarrierException
await in class
CyclicBarrier
InterruptedException
BrokenBarrierException
public int await(long timeout,
TimeUnit unit)
throws InterruptedException,
BrokenBarrierException,
TimeoutException
await in class
CyclicBarrier
InterruptedException
BrokenBarrierException
TimeoutException
public void reset(Throwablecause)
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.
cause - The cause of the BrokenBarrierException
public boolean isBroken()
reset(Throwable) is invoked the barrier will remain broken, while
CyclicBarrier.reset() will reset the barrier to its initial state and
isBroken() will return false.
isBroken in class
CyclicBarrier
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.
inspect()
public void inspect()
throws BrokenBarrierException
BrokenBarrierException will be thrown. Otherwise, would return gracefully.
BrokenBarrierException - With a nested broken cause.