diff --git a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/utils/CurrentLock.java b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/utils/CurrentLock.java new file mode 100644 index 000000000..f2c8e55e7 --- /dev/null +++ b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/utils/CurrentLock.java @@ -0,0 +1,49 @@ +package org.nl.acs.utils; + +import cn.hutool.core.util.StrUtil; + +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; + +public class CurrentLock { + + static ConcurrentHashMap clock = new ConcurrentHashMap<>(); + static Timer timer = new Timer(true); + + public static Boolean tryLock(String key){ + synchronized (buildLock(key)){ + ReentrantLock lock = clock.get(key); + if (lock == null){ + ReentrantLock reentrantLock = new ReentrantLock(); + clock.put(key,reentrantLock); + return reentrantLock.tryLock(); + }else { + return lock.tryLock(); + } + } + } + public static void unLock(String key){ + System.out.println("---释放锁---"); + synchronized (buildLock(key)){ + ReentrantLock lock = clock.get(key); + if (lock != null){ + if (lock.isLocked() && lock.isHeldByCurrentThread()){ + lock.unlock(); + } + } + } + } + private static String buildLock(String key) { + if (StrUtil.isEmpty(key)) { + key = ""; + } + StringBuilder builder = new StringBuilder(); + builder.append("currentLock."); + builder.append(key); + String lock = builder.toString().intern(); + return lock; + } +}