From eeb86aaf2f73a02600195ce2637dde6caf858a88 Mon Sep 17 00:00:00 2001
From: T100738 <1821349743@qq.com>
Date: 星期二, 16 四月 2024 22:30:34 +0800
Subject: [PATCH] commit

---
 product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java |  179 +++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 119 insertions(+), 60 deletions(-)

diff --git a/product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java b/product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java
index b1f1981..6336553 100644
--- a/product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java
+++ b/product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java
@@ -1,12 +1,18 @@
 package com.product.data.center.utils;
 
 import cn.hutool.core.collection.ConcurrentHashSet;
+import cn.hutool.core.date.DateUtil;
 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 javax.swing.*;
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * @Author cheng
@@ -16,87 +22,140 @@
 public class CustomLock {
 
 
-    private Set<String> obj = new ConcurrentHashSet<>();
+	private Map<String, Long> obj = new ConcurrentHashMap<>();
+	private Map<String, Long> threadObj = new ConcurrentHashMap<>();
 
 
-    private long detectionWaitTime = 1000L;
+	private long detectionWaitTime = 1000L;
 
 
-    /**
-     * 鏃犲弬鏋勯�犳椂
-     */
-    public CustomLock() {
-    }
+	/**
+	 * 鏃犲弬鏋勯�犳椂
+	 */
+	public CustomLock() {
+	}
 
-    /**
-     * @param detectionWaitTime 妫�娴嬬瓑寰呮椂闂达紝鍗曚綅姣
-     */
-    public CustomLock(long detectionWaitTime) {
-        this.detectionWaitTime = detectionWaitTime;
-    }
+	/**
+	 * @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) {
+		return this.tryLock(ArrayUtil.join(key, ","));
+	}
 
-    public boolean tryLock(String key) {
-        if (!StringUtils.isEmpty(key)) {
-            key = key.toUpperCase();
-            synchronized (key.intern()) {
-                if (!this.obj.contains(key)) {
-                    lock(key);
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
+	public static void main(String[] args) {
+		long timemillis = 1709654400039L;
+		//杞崲涓哄勾鏈堟棩鏃跺垎绉�
+		String format = DateUtil.format(DateUtil.date(timemillis), "yyyy-MM-dd HH:mm:ss");
+
+		System.out.println(format);
+	}
+
+	public boolean tryLock(String key, long timeoutMinute) {
+		if (!StringUtils.isEmpty(key)) {
+			key = key.toUpperCase();
+			synchronized (key.intern()) {
+				if (!this.obj.containsKey(key)) {
+					lock(key);
+					//鑾峰彇褰撳墠绾跨▼
+					long id = Thread.currentThread().getId();
+					threadObj.put(key, id);
+					return true;
+				} else if (timeoutMinute > 0) {
+					//鑾峰彇閿佽繘鍏ュ紑濮嬫椂闂�
+					long startTime = obj.get(key);
+					//鑾峰彇褰撳墠鏃堕棿
+					long currentTime = System.currentTimeMillis();
+
+					//灏嗗垎閽熻浆鎹负姣
+					long timeout = timeoutMinute * 60 * 1000;
+					//濡傛灉褰撳墠鏃堕棿鍑忓幓寮�濮嬫椂闂村ぇ浜庣瓑浜庤秴鏃舵椂闂�
+					if (currentTime - startTime >= timeout) {
+						long threadId = threadObj.get(key);
+						//鏍规嵁绾跨▼id鑾峰彇绾跨▼
+						Thread[] threads = ThreadUtil.getThreads();
+						for (Thread thread : threads) {
+							if (thread.getId() == threadId) {
+								//涓柇绾跨▼
+								ThreadUtil.interrupt(thread, true);
+								SpringMVCContextHolder.getSystemLogger().error("绾跨▼锛�" + thread.getName() + "瓒呮椂琚腑鏂�");
+								return tryLock(key, timeoutMinute);
+							}
+						}
+						return true;
+					}
+
+				}
+			}
+		}
+		return false;
+	}
 
 
-    public void lock(String... key) {
-        this.lock(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);
+					//鑾峰彇褰撳墠绾跨▼
+					long id = Thread.currentThread().getId();
+					threadObj.put(key, id);
+					return true;
+				}
+			}
+		}
+		return false;
+	}
 
-    public void lock(String key) {
-        String obj = key;
-        if (StringUtils.isEmpty(obj)) {
-            return;
-        }
-        obj = obj.toUpperCase();
+
+	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.contains(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);
-                }
-            }
+		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);
-        }
+			ThreadUtil.sleep(this.detectionWaitTime);
+		}
 //        if (enterLock) {
 //            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) {
-        String obj = ArrayUtil.join(key, ",");
-        unLock(obj);
-    }
+	public void unLock(String... key) {
+		String obj = ArrayUtil.join(key, ",");
+		unLock(obj);
+	}
 
-    public void unLock(String key) {
+	public void unLock(String key) {
 
-        if (StringUtils.isEmpty(key)) {
-            return;
-        }
-        this.obj.remove(key.toUpperCase());
+		if (StringUtils.isEmpty(key)) {
+			return;
+		}
+		this.obj.remove(key.toUpperCase());
 //        SpringMVCContextHolder.getSystemLogger().info("閲婃斁閿佹垚鍔�,lockKey:" + key);
-    }
+	}
 
 
 }

--
Gitblit v1.9.2