public class CloseableLock extends Object implements AutoCloseable
A shared lock can be acquired if any other shared locks are also acquired. An exclusive lock acquire will block until all shared locks have been released.
Locks are re-entrant; trying to acquire another lock (of the same type) while a lock is already held will immediately succeed.
Acquiring to acquire a shared lock while holding an exclusive lock or vice versa is not
supported; attempting it will throw an IllegalStateException
.
If the lock is closed, all future and current acquires will immediately return null
.
Modifier and Type | Class and Description |
---|---|
class |
CloseableLock.ScopedLock
Helper class to release a lock at the end of a try-with-resources statement.
|
Constructor and Description |
---|
CloseableLock()
Create a new instance; starts out with 0 locks acquired.
|
CloseableLock(String name)
Create a new instance; starts out with 0 locks acquired.
|
Modifier and Type | Method and Description |
---|---|
CloseableLock.ScopedLock |
acquireExclusiveLock()
Try to acquire the lock exclusively, blocking until all other threads release their locks.
|
CloseableLock.ScopedLock |
acquireLock()
Try to acquire the lock non-exclusively, blocking until the operation completes.
|
void |
close()
Acquires the lock exclusively (blocking), marks it as closed, then releases the lock.
|
void |
releaseLock()
Release a single lock that was acquired.
|
public CloseableLock()
public CloseableLock(String name)
name
- set an optional name for logging functionalitypublic void close()
Marking a lock as closed will fail all further acquisition attempts; it will also immediately unblock all other threads currently trying to acquire a lock.
This operation is idempotent; calling it more than once has no effect.
close
in interface AutoCloseable
IllegalStateException
- if an attempt is made to close
while this thread has a lock acquiredpublic CloseableLock.ScopedLock acquireLock()
If the lock has already been closed, or being closed before this operation returns,
the call will immediately return false
.
If other threads hold a non-exclusive lock (and the lock is not yet closed), this operation will return immediately. If another thread holds an exclusive lock, this thread will block until the exclusive lock has been released.
This lock is re-entrant; acquiring more than one non-exclusive lock per thread is
supported, and must be matched by an equal number of releaseLock()
calls.
ScopedLock
instance if the lock was acquired, or null
if the lock
was already closed.IllegalStateException
- if this thread is already holding an exclusive lockpublic CloseableLock.ScopedLock acquireExclusiveLock()
If the lock has already been closed, or being closed before this operation returns,
the call will immediately return false
.
If any other threads are holding a lock, this thread will block until all other locks are released.
This lock is re-entrant; acquiring more than one exclusive lock per thread is supported,
and must be matched by an equal number of releaseLock()
calls.
ScopedLock
instance if the lock was acquired, or null
if the lock
was already closed.IllegalStateException
- if an attempt is made to acquire an exclusive lock while already holding a lockpublic void releaseLock()
Any other other that is blocked and trying to acquire a lock will get a chance to acquire the lock.
IllegalStateException
- if no locks were acquired, or if the lock was already closed