public interface Locking
For read-write and stamped locks, operations are also provided for demoting a held write lock to a read lock. Stamped locks also include operations for promoting a read lock to a write lock and for performing optimistic reads.
Modifier and Type | Interface and Description |
---|---|
static interface |
Locking.ReadWriteLocking
An interface that encapsulates typical patterns for read-write locks.
|
static interface |
Locking.StampedLocking
An interface that encapsulates typical patterns for stamped locks.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
callWithLock(Callable<T> action)
Invokes the given action while holding a lock.
|
static Locking |
forLock(Lock lock)
Returns a locking interface for the given lock.
|
static Locking.ReadWriteLocking |
forReadWriteLock(ReadWriteLock lock)
Returns a locking interface for the given read-write lock.
|
static Locking.StampedLocking |
forStampedLock(StampedLock lock)
Returns a locking interface for the given stamped lock.
|
void |
runWithLock(Runnable action)
Runs the given action while holding a lock.
|
void runWithLock(Runnable action)
action
- the action to run<T> T callWithLock(Callable<T> action) throws Exception
action
- the action to runException
- if the given action throws an actionstatic Locking forLock(Lock lock)
ReadWriteLock
:// BAD. NOT the recommended way: Locking readLock = Locking.forLock(rwLock.readLock()); // GOOD. This is the recommended alternative: Locking readLock = Locking.forReadWriteLock(rwLock).forReadLock();
lock
- the lock that is held when actions are invoked through the interfacestatic Locking.ReadWriteLocking forReadWriteLock(ReadWriteLock lock)
lock
- a read-write lockstatic Locking.StampedLocking forStampedLock(StampedLock lock)
lock
- a stamped lock