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 | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 57 insertions(+), 0 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 331802b..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,11 +1,15 @@ 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; @@ -19,6 +23,7 @@ private Map<String, Long> obj = new ConcurrentHashMap<>(); + private Map<String, Long> threadObj = new ConcurrentHashMap<>(); private long detectionWaitTime = 1000L; @@ -41,12 +46,64 @@ return this.tryLock(ArrayUtil.join(key, ",")); } + 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 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; } } -- Gitblit v1.9.2