锁工具类
public class LockSyncUtil {
private static final ConcurrentHashMap<String, Lock> lockPool = new ConcurrentHashMap<>();
/**
* 用来锁定获取锁的锁
*/
private static final Lock lock = new ReentrantLock();
//获得锁
public static Lock getLock(String str) {
try {
lock.lock();
Lock mapLock = lockPool.get(str);
if (Objects.isNull(mapLock)) {
mapLock = new ReentrantLock();
lockPool.put(str,mapLock);
}
return mapLock;
} finally {
lock.unlock();
}
}
public static Lock unLock(String str) {
Lock lock = lockPool.get(str);
if (Objects.nonNull(lock)) {
ReentrantLock reentrantLock = (ReentrantLock) lock;
if (!reentrantLock.isLocked()) {
lockPool.remove(str);
}
}
return lock;
}
}