edu.rice.cs.plt.concurrent
Class LockMap<T>
java.lang.Object
edu.rice.cs.plt.concurrent.LockMap<T>
public class LockMap<T>
- extends Object
A map for associating arbitrary objects with locks for the lifetime of the objects. This allows, for example,
synchronization based on the arguments to a method (the alternative of directly locking on the arguments
themselves is dangerous, because the argument objects may be used for locking in a different context; two
threads locking on the same object for unrelated purposes may lead to deadlock). Each unique key
(distinguished via ==) is associated with a different ReentrantLock. Weak references are used to
allow keys to be freely garbage-collected.
|
Method Summary |
ReentrantLock |
get(T val)
Get the lock associated with the given value. |
Runnable |
lock(T val)
Acquire the lock associated with the given value, and return a Runnable for unlocking when the lock can
be released. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
LockMap
public LockMap()
LockMap
public LockMap(int initialCapacity)
get
public ReentrantLock get(T val)
- Get the lock associated with the given value. If necessary, allocate and cache a new lock.
lock
public Runnable lock(T val)
- Acquire the lock associated with the given value, and return a Runnable for unlocking when the lock can
be released. Clients should generally follow this invocation with a
try/finally block that
guarantees execution of the resulting runnable.
- Returns:
- A Runnable for invoking
lock.unlock(). If invoked incorrectly, may throw an
IllegalMonitorStateException. - See Also:
ReentrantLock.lock(),
ReentrantLock.unlock()