Contents
- Description
- Nested Class Summary
- Field Summary
- Constructor Summary
- Method Summary
- Field Details
- Constructor Details
- Method Details
- tryAcquireExclusive()
- tryAcquireExclusiveNanos(long)
- acquireExclusive()
- acquireExclusiveInterruptibly()
- uponExclusive(Runnable)
- downgrade()
- releaseExclusive()
- release(boolean)
- releaseEither()
- tryAcquireShared()
- tryAcquireSharedNanos(long)
- acquireSharedUncontended()
- acquireSharedUncontendedNanos(long)
- acquireShared()
- acquireSharedInterruptibly()
- tryUpgrade()
- releaseShared()
- hasQueuedThreads()
- toString()
Class Latch
java.lang.Object
org.cojen.tupl.util.Latch
- Direct Known Subclasses:
Clutch
,Clutch.Pack
Non-reentrant read-write latch, designed for throughput over fairness. Implementation
doesn't track thread ownership or check for illegal usage. As a result, it typically
outperforms ReentrantLock and built-in Java synchronization. Although latch acquisition is
typically unfair, waiting threads aren't starved indefinitely.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Manages a queue of waiting threads, associated with aLatch
instance. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Acquire the exclusive latch, barging ahead of any waiting threads if possible.void
Acquire the exclusive latch, aborting if interrupted.void
Acquire a shared latch, barging ahead of any waiting threads if possible.void
Acquire a shared latch, aborting if interrupted.boolean
Like tryAcquireShared, except blocks if an exclusive latch is held.int
acquireSharedUncontendedNanos
(long nanosTimeout) Like tryAcquireSharedNanos, except blocks if an exclusive latch is held.final void
Downgrade the held exclusive latch into a shared latch.final boolean
final void
release
(boolean exclusive) Convenience method, which releases the held exclusive or shared latch.final void
Releases an exclusive or shared latch.final void
Release the held exclusive latch.void
Release a held shared latch.toString()
boolean
Try to acquire the exclusive latch, barging ahead of any waiting threads if possible.boolean
tryAcquireExclusiveNanos
(long nanosTimeout) Attempt to acquire the exclusive latch, aborting if interrupted.boolean
Try to acquire a shared latch, barging ahead of any waiting threads if possible.boolean
tryAcquireSharedNanos
(long nanosTimeout) Attempt to acquire a shared latch, aborting if interrupted.boolean
Attempt to upgrade a held shared latch into an exclusive latch.void
uponExclusive
(Runnable cont) Invokes the given continuation upon the latch being acquired exclusively.
-
Field Details
-
Constructor Details
-
Method Details
-
tryAcquireExclusive
public boolean tryAcquireExclusive()Try to acquire the exclusive latch, barging ahead of any waiting threads if possible. -
tryAcquireExclusiveNanos
Attempt to acquire the exclusive latch, aborting if interrupted.- Parameters:
nanosTimeout
- pass negative for infinite timeout- Throws:
InterruptedException
-
acquireExclusive
public void acquireExclusive()Acquire the exclusive latch, barging ahead of any waiting threads if possible. -
acquireExclusiveInterruptibly
Acquire the exclusive latch, aborting if interrupted.- Throws:
InterruptedException
-
uponExclusive
Invokes the given continuation upon the latch being acquired exclusively. When acquired, the continuation is run by the current thread, or it's enqueued to be run by a thread which releases the latch. The releasing thread actually retains the latch and runs the continuation, effectively transferring latch ownership. The continuation must not explicitly release the latch, although it can downgrade the latch. Any exception thrown by the continuation is passed to the uncaught exception handler of the running thread, and then the latch is released.- Parameters:
cont
- called with latch held
-
downgrade
public final void downgrade()Downgrade the held exclusive latch into a shared latch. Caller must later call releaseShared instead of releaseExclusive. -
releaseExclusive
public final void releaseExclusive()Release the held exclusive latch. -
release
public final void release(boolean exclusive) Convenience method, which releases the held exclusive or shared latch.- Parameters:
exclusive
- call releaseExclusive if true, else call releaseShared
-
releaseEither
public final void releaseEither()Releases an exclusive or shared latch. -
tryUpgrade
public boolean tryUpgrade()Attempt to upgrade a held shared latch into an exclusive latch. Upgrade fails if shared latch is held by more than one thread. If successful, caller must later call releaseExclusive instead of releaseShared. -
hasQueuedThreads
public final boolean hasQueuedThreads() -
toString
-