To understand intuitively how this class works, the idea is simply that a thread, once it considers itself incapable of making progress, registers to be awoken once that changes. Since this could have changed between checking and registering (in which case the thread that made this change would have been unable to signal it), it checks the condition again, sleeping only if it hasn't changed/still is not met.
This thread synchronisation scheme has some advantages over Condition objects and Object.wait/notify in that no monitor acquisition is necessary and, in fact, besides the actual waiting on a signal, all operations are non-blocking. As a result consumers can never block producers, nor each other, or vice versa, from making progress. Threads that are signalled are also put into a RUNNABLE state almost simultaneously, so they can all immediately make progress without having to serially acquire the monitor/lock, reducing scheduler delay incurred.
A few notes on utilisation:
1. A thread will only exit await() when it has been signalled, but this does not guarantee the condition has not been altered since it was signalled, and depending on your design it is likely the outer condition will need to be checked in a loop, though this is not always the case.
2. Each signal is single use, so must be re-registered after each await(). This is true even if it times out.
3. If you choose not to wait on the signal (because the condition has been met before you waited on it) you must cancel() the signal if the signalling thread uses signal() to awake waiters; otherwise signals will be lost. If signalAll() is used but infrequent, and register() is frequent, cancel() should still be used to prevent the queue growing unboundedly. Similarly, if you provide a TimerContext, cancel should be used to ensure it is not erroneously counted towards wait time.
4. Care must be taken when selecting conditionMet() to ensure we are waiting on the condition that actually indicates progress is possible. In some complex cases it may be tempting to wait on a condition that is only indicative of local progress, not progress on the task we are aiming to complete, and a race may leave us waiting for a condition to be met that we no longer need.
5. This scheme is not fair
6. Only the thread that calls register() may call await()
|
|
|
|
|
|