org.apache.qpid.util.concurrent
Class BooleanLatch
java.lang.Object
org.apache.qpid.util.concurrent.BooleanLatch
public class BooleanLatch
- extends Object
A BooleanLatch is like a set of traffic lights, where threads can wait at a red light until another thread gives
the green light. When threads arrive at the latch it is initially red. They queue up until the green signal is
given, at which point they can all acquire the latch in shared mode and continue to run concurrently. Once the latch
is signalled it cannot be reset to red again.
The latch uses a AbstractQueuedSynchronizer
to implement its synchronization.
This has two internal states, 0 which means that the latch is blocked, and 1 which means that the latch is open.
CRC Card
Responsibilities | Collaborations
|
---|
Block threads until a go signal is given.
|
- Todo:
- Might be better to use a countdown latch to count down from 1. Its await method can throw interrupted
exception which makes the possibility of interruption more explicit, and provides a reminder to recheck the
latch condition before continuing.
Method Summary |
void |
await()
Waits on the latch until the signal is given and the light is green. |
boolean |
isSignalled()
Tests whether or not the latch has been signalled, that is to say that, the light is green. |
void |
signal()
Releases any threads currently waiting on the latch. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
BooleanLatch
public BooleanLatch()
isSignalled
public boolean isSignalled()
- Tests whether or not the latch has been signalled, that is to say that, the light is green.
This method is non-blocking.
- Returns:
- true if the latch may be acquired; the light is green.
await
public void await()
- Waits on the latch until the signal is given and the light is green. If the light is already green then the
latch will be acquired and the thread will not have to wait.
This method will block until the go signal is given or the thread is otherwise interrupted. Before carrying
out any processing threads that return from this method should confirm that the go signal has really been given
on this latch by calling the
isSignalled()
method.
signal
public void signal()
- Releases any threads currently waiting on the latch. This flips the light to green allowing any threads that
were waiting for this condition to now run.
This method is non-blocking.
Licensed to the Apache Software Foundation