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 classManages a queue of waiting threads, associated with aLatchinstance. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAcquire the exclusive latch, barging ahead of any waiting threads if possible.voidAcquire the exclusive latch, aborting if interrupted.voidAcquire a shared latch, barging ahead of any waiting threads if possible.voidAcquire a shared latch, aborting if interrupted.booleanLike tryAcquireShared, except blocks if an exclusive latch is held.intacquireSharedUncontendedNanos(long nanosTimeout) Like tryAcquireSharedNanos, except blocks if an exclusive latch is held.final voidDowngrade the held exclusive latch into a shared latch.final booleanfinal voidrelease(boolean exclusive) Convenience method, which releases the held exclusive or shared latch.final voidReleases an exclusive or shared latch.final voidRelease the held exclusive latch.voidRelease a held shared latch.toString()booleanTry to acquire the exclusive latch, barging ahead of any waiting threads if possible.booleantryAcquireExclusiveNanos(long nanosTimeout) Attempt to acquire the exclusive latch, aborting if interrupted.booleanTry to acquire a shared latch, barging ahead of any waiting threads if possible.booleantryAcquireSharedNanos(long nanosTimeout) Attempt to acquire a shared latch, aborting if interrupted.booleanAttempt to upgrade a held shared latch into an exclusive latch.voiduponExclusive(Runnable cont) Invokes the given continuation upon the latch being acquired exclusively.
-
Field Details
-
UNLATCHED
public static final int UNLATCHED- See Also:
-
EXCLUSIVE
public static final int EXCLUSIVE- See Also:
-
SHARED
public static final int SHARED- See Also:
-
-
Constructor Details
-
Latch
public Latch() -
Latch
public Latch(int initialState) - Parameters:
initialState- UNLATCHED, EXCLUSIVE, or SHARED
-
-
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
-