package com.product.data.center.utils; import cn.hutool.core.collection.ConcurrentHashSet; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.ArrayUtil; 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 * @Date 2022/11/14 11:30 * @Desc */ public class CustomLock { private Map obj = new ConcurrentHashMap<>(); private long detectionWaitTime = 1000L; /** * 无参构造时 */ public CustomLock() { } /** * @param detectionWaitTime 检测等待时间,单位毫秒 */ public CustomLock(long detectionWaitTime) { this.detectionWaitTime = detectionWaitTime; } public boolean tryLock(String[] key) { return this.tryLock(ArrayUtil.join(key, ",")); } public boolean tryLock(String key) { if (!StringUtils.isEmpty(key)) { key = key.toUpperCase(); synchronized (key.intern()) { if (!this.obj.containsKey(key)) { lock(key); return true; } } } return false; } public void lock(String... key) { this.lock(ArrayUtil.join(key, ",")); } public void lock(String key) { String obj = key; if (StringUtils.isEmpty(obj)) { return; } obj = obj.toUpperCase(); // boolean enterLock = false; long starTime = System.currentTimeMillis(); Long outLogTime = null; 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().error("等待释放锁时间超过5分钟,等待时间:" + (System.currentTimeMillis() - starTime) + "ms,lockKey:" + obj); } } // enterLock = true; // SpringMVCContextHolder.getSystemLogger().info("锁释放等待,lockKey:" + obj); ThreadUtil.sleep(this.detectionWaitTime); } // if (enterLock) { // SpringMVCContextHolder.getSystemLogger().info("等待定时间:" + (System.currentTimeMillis() - starTime) + " ms,lockKey:" + obj); // } // SpringMVCContextHolder.getSystemLogger().info("上锁成功,lockKey:" + obj); this.obj.put(obj, System.currentTimeMillis()); } public void unLock(String... key) { String obj = ArrayUtil.join(key, ","); unLock(obj); } public void unLock(String key) { if (StringUtils.isEmpty(key)) { return; } this.obj.remove(key.toUpperCase()); // SpringMVCContextHolder.getSystemLogger().info("释放锁成功,lockKey:" + key); } }