public class CompeteLatch
extends Object
await
. After the winner thread finishes its job, it should call
done
which will open the latch. All blocking loser threads can
pass the latch at the same time.
See LPS-3744 for a sample use case.
Constructor and Description |
---|
CompeteLatch() |
Modifier and Type | Method and Description |
---|---|
void |
await()
This method should only be called by a loser thread.
|
boolean |
await(long timeout,
TimeUnit timeUnit)
This method should only be called by a loser thread.
|
boolean |
compete()
Tells the current thread to join the competition.
|
boolean |
done()
This method should only be called by the winner thread.
|
boolean |
isLocked()
Returns
true if the latch is locked. |
public void await() throws InterruptedException
InterruptedException
- if the current thread is interruptedpublic boolean await(long timeout, TimeUnit timeUnit) throws InterruptedException
timeout
- the timeout valuetimeUnit
- the time unittrue
if the latch was open, false
if
the waiting time elapsed before the latch be opened.InterruptedException
- if the current thread is interruptedpublic boolean compete()
true
if the current thread is the winner threadpublic boolean done()
await
method. If a loser thread does call this method when a winner thread has
locked the latch, the latch will break and the winner thread may be put
into a non thread safe state. You should never have to do this except to
get out of a deadlock. If no one threads have locked the latch, then
calling this method has no effect. This method will return immediately.true
if this call opens the latch,
false
if the latch is already openpublic boolean isLocked()
true
if the latch is locked. This method should not
be used to test the latch before joining a competition because it is not
thread safe. The only purpose for this method is to give external systems
a way to monitor the latch which is usually be used for deadlock
detection.true
if the latch is locked; false
otherwise