public class Latch extends Object
await()
any time after the counter has
been incremented. They will not be released until the counter is zero. The
typical pattern is to incrementing the counter before some operation and the
decrement the counter after that operation. This pattern may be safely used
in combination with nested invocations and with concurrent threads.
Note: This class is very similar to a CountDownLatch
, however the
counter maximum is not specified in advance.
Modifier and Type | Field and Description |
---|---|
protected static org.apache.log4j.Logger |
log |
Constructor and Description |
---|
Latch() |
Latch(ReentrantLock lock) |
Latch(String name,
ReentrantLock lock) |
Modifier and Type | Method and Description |
---|---|
long |
addAndGet(long delta)
Adds the delta to the internal counter.
|
void |
await()
Await the counter to become zero unless interrupted.
|
boolean |
await(long timeout,
TimeUnit unit)
Await the counter to become zero, but no longer than the timeout.
|
long |
dec()
Decrements the internal counter and releases the blocked thread(s) if the
counter reaches zero.
|
long |
get()
The counter value (peek at current value without obtaining the lock).
|
long |
inc()
Increments the internal counter.
|
protected void |
signal()
Invoked when the latch reaches zero after any threads blocked at
await(long, TimeUnit) have been released. |
String |
toString() |
public Latch()
public Latch(ReentrantLock lock)
lock
- The lock to be used (optional). When none is specified, a new
lock will be allocated. The caller MAY choose to specify the
lock in order to avoid nested lock designs by using the same
lock inside the Latch
and in some outer context.public Latch(String name, ReentrantLock lock)
name
- An optional name that will be displayed by toString()
along with the current counter value.lock
- The lock to be used (optional). When none is specified, a new
lock will be allocated. The caller MAY choose to specify the
lock in order to avoid nested lock designs by using the same
lock inside the Latch
and in some outer context.public long get()
public long inc()
public long addAndGet(long delta)
public long dec()
IllegalStateException
- if the counter would become negative.protected void signal() throws InterruptedException
await(long, TimeUnit)
have been released. This may be overridden
to perform additional processing, such as moving an associated object
onto another queue.
CAUTION: DO NOT invoke any operation from within this method which could block as that would cause the thread running the asynchronous write task in which this method is invoked to block. If you are transferring objects to a queue, the queue MUST be unbounded.
InterruptedException
public void await() throws InterruptedException
InterruptedException
public boolean await(long timeout, TimeUnit unit) throws InterruptedException
timeout
- The timeout.unit
- The unit in which the timeout is expressed.true
if the counter reached zero and
false
if the timeout was exceeded before the counter
reached zero.InterruptedException
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.