public class LockResource extends Object implements Closeable
try (LockResource r = new LockResource(lock)) { ... }
Constructor and Description |
---|
LockResource(Lock lock)
Creates a new instance of
LockResource using the given lock. |
LockResource(Lock lock,
boolean acquireLock,
boolean useTryLock)
Creates a new instance of
LockResource using the given lock. |
LockResource(Lock lock,
boolean acquireLock,
boolean useTryLock,
Runnable closeAction)
Creates a new instance of
LockResource using the given lock. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Releases the lock.
|
boolean |
hasSameLock(LockResource other)
Returns true if the other
LockResource contains the same lock. |
protected Lock mLock
public LockResource(Lock lock)
LockResource
using the given lock.lock
- the lock to acquirepublic LockResource(Lock lock, boolean acquireLock, boolean useTryLock)
LockResource
using the given lock.
This method may use the Lock.tryLock()
method to gain ownership of the locks. The
reason one might want to use this is to avoid the fairness heuristics within the
ReentrantReadWriteLock
's NonFairSync which may block reader
threads if a writer if the first in the queue.lock
- the lock to acquireacquireLock
- whether to lock the lockuseTryLock
- whether or not use to Lock.tryLock()
public LockResource(Lock lock, boolean acquireLock, boolean useTryLock, @Nullable Runnable closeAction)
LockResource
using the given lock.
This method may use the Lock.tryLock()
method to gain ownership of the locks. The
reason one might want to use this is to avoid the fairness heuristics within the
ReentrantReadWriteLock
's NonFairSync which may block reader
threads if a writer if the first in the queue.lock
- the lock to acquireacquireLock
- whether to lock the lockuseTryLock
- whether or not use to Lock.tryLock()
closeAction
- the nullable closeable that will be run before releasing the lockpublic boolean hasSameLock(LockResource other)
LockResource
contains the same lock.other
- other LockResourcepublic void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2023. All Rights Reserved.