| | |
| | | import com.product.core.spring.context.SpringMVCContextHolder; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * @Author cheng |
| | |
| | | public class CustomLock { |
| | | |
| | | |
| | | private Set<String> obj = new ConcurrentHashSet<>(); |
| | | private Map<String, Long> obj = new ConcurrentHashMap<>(); |
| | | |
| | | |
| | | private long detectionWaitTime = 1000L; |
| | |
| | | if (!StringUtils.isEmpty(key)) { |
| | | key = key.toUpperCase(); |
| | | synchronized (key.intern()) { |
| | | if (!this.obj.contains(key)) { |
| | | if (!this.obj.containsKey(key)) { |
| | | lock(key); |
| | | return true; |
| | | } |
| | |
| | | // boolean enterLock = false; |
| | | long starTime = System.currentTimeMillis(); |
| | | Long outLogTime = null; |
| | | while (this.obj.contains(obj)) { |
| | | while (this.obj.containsKey(obj)) { |
| | | if (System.currentTimeMillis() - starTime >= (1000 * 60 * 5)) { |
| | | if (outLogTime == null || System.currentTimeMillis() - outLogTime >= (1000 * 60)) { |
| | | outLogTime = System.currentTimeMillis(); |
| | |
| | | // SpringMVCContextHolder.getSystemLogger().info("等待定时间:" + (System.currentTimeMillis() - starTime) + " ms,lockKey:" + obj); |
| | | // } |
| | | // SpringMVCContextHolder.getSystemLogger().info("上锁成功,lockKey:" + obj); |
| | | this.obj.add(obj); |
| | | this.obj.put(obj, System.currentTimeMillis()); |
| | | } |
| | | |
| | | public void unLock(String... key) { |