blob: 4d9f45e959e26f279f8893c6a321ebd1620fcad1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.yahoo.concurrent.lock;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author valerijf
*/
public class Locking {
private final Map<Class<?>, ReentrantLock> locks = new ConcurrentHashMap<>();
/**
* Locks class. This will block until the lock is acquired.
* Users of this <b>must</b> close any lock acquired.
*
* @param key the key to lock
* @return the acquired lock
*/
public Lock lock(Class<?> key) {
ReentrantLock lock = locks.computeIfAbsent(key, k -> new ReentrantLock(true));
lock.lock();
return new Lock(lock);
}
}
|