public class NamedLocks extends Object
NamedLocks locks = new NamedLocks(); synchronized (locks.acquire(key)) { // Synchronized for all locks named key }Keys must implement hashCode() and equals() correctly. Lock objects are weak references thus GCed once no longer reachable from code. You should not keep hard references to the acquired lock outside of the synchronized block.
Constructor and Description |
---|
NamedLocks()
Constructs the lock holder.
|
Modifier and Type | Method and Description |
---|---|
Object |
acquire(Object key)
Acquires a lock for the specified key.
|
static void |
lockOnce(String name) |
int |
size() |
static void |
unlockOnce(String name) |
public Object acquire(Object key)
key
- public int size()
public static void lockOnce(String name)
public static void unlockOnce(String name)
Copyright © 2017. All rights reserved.