From eb26f811410d5afbca2d02253507389e47daaa4b Mon Sep 17 00:00:00 2001
From: lsh <1>
Date: 星期五, 05 一月 2024 12:44:28 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/SiemensCrnThread.java | 14 ++++++++++++--
src/main/java/com/zy/Boot.java | 2 ++
src/main/java/com/zy/core/ServerBootstrap.java | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 56 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/zy/Boot.java b/src/main/java/com/zy/Boot.java
index f25a356..a616580 100644
--- a/src/main/java/com/zy/Boot.java
+++ b/src/main/java/com/zy/Boot.java
@@ -5,8 +5,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync
+@EnableScheduling
@SpringBootApplication
public class Boot extends SpringBootServletInitializer {
diff --git a/src/main/java/com/zy/core/ServerBootstrap.java b/src/main/java/com/zy/core/ServerBootstrap.java
index dfca756..0a6396f 100644
--- a/src/main/java/com/zy/core/ServerBootstrap.java
+++ b/src/main/java/com/zy/core/ServerBootstrap.java
@@ -12,10 +12,13 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
* Created by vincent on 2020/8/4
@@ -28,6 +31,9 @@
private SlaveProperties slaveProperties;
@Autowired
private MainProcess mainProcess;
+
+ public static final Map<CrnThread, Thread> map = new ConcurrentHashMap<>();
+
/**
* PostConstruct浼氬湪鍔犺浇servlet鐨勬椂鍊欒繍琛屼竴娆�
@@ -129,5 +135,41 @@
public void destroy() {
}
+ @Component
+ class killThread {
+
+ @Scheduled(cron = "0/3 * * * * ? ")
+ public void kill() {
+ int i = 0;
+ for (Map.Entry<CrnThread, Thread> entry : map.entrySet()) {
+ i++;
+ SiemensCrnThread key = (SiemensCrnThread) entry.getKey();
+// System.out.println("绗�"+i+"涓爢鍨涙満绾跨▼閲嶅惎绾跨▼鏉′欢锛�"+(System.currentTimeMillis() - key.getSign()));
+ if (System.currentTimeMillis() - key.getSign() > 120000) {
+ System.out.println("绗�"+i+"涓爢鍨涙満绾跨▼琚噸鍚�");
+
+ // 璇锋眰绾跨▼瀹夊叏鍋滄
+ key.requestStop();
+
+ // 绛夊緟绾跨▼瀹屾垚鍋滄, 鍙互杩欐牱鍋氫篃鍙互閫夋嫨涓嶈繖涔堝仛锛屽彇鍐充簬鏄惁闇�瑕佺瓑寰呯嚎绋嬬粨鏉�
+ try {
+ entry.getValue().join(20000); // 绛夊緟鏈�澶�20绉�
+ } catch (InterruptedException e) {
+// // 鍙�傚綋澶勭悊涓柇
+// Thread.currentThread().interrupt(); // 淇濈暀涓柇鐘舵��
+ entry.getValue().stop();
+ }
+
+ // 閲嶆柊鍚姩绾跨▼
+ Thread thread = new Thread((Runnable) key); // 鍋囪CrnThread瀹炵幇浜哛unnable
+ thread.start();
+
+ // 鏇挎崲鏄犲皠鍏崇郴涓殑鏃х嚎绋�
+ map.put(entry.getKey(), thread);
+ }
+ }
+ }
+
+ }
}
diff --git a/src/main/java/com/zy/core/thread/SiemensCrnThread.java b/src/main/java/com/zy/core/thread/SiemensCrnThread.java
index 5072272..3da01f5 100644
--- a/src/main/java/com/zy/core/thread/SiemensCrnThread.java
+++ b/src/main/java/com/zy/core/thread/SiemensCrnThread.java
@@ -39,6 +39,10 @@
private CrnProtocol crnProtocol;
private boolean resetFlag = false;
+ public Long sign = System.currentTimeMillis();
+
+ public boolean isRunning = true;
+
/**
* 鍫嗗灈鏈烘槸鍚﹀湪鍥炲師鐐硅繍鍔ㄤ腑鏍囪
*/
@@ -52,7 +56,7 @@
@SuppressWarnings("InfiniteLoopStatement")
public void run() {
this.connect();
- while (true) {
+ while (isRunning) {
try {
int step = 1;
Task task = MessageQueue.poll(SlaveType.Crn, slave.getId());
@@ -93,7 +97,8 @@
} catch (Exception e) {
// e.printStackTrace();
}
-
+// System.out.println("绗�"+slave.getId()+"涓爢鍨涙満绾跨▼杩愯涓�杞細"+(System.currentTimeMillis()-sign));
+ sign = System.currentTimeMillis();
}
}
@@ -415,4 +420,9 @@
}
+ // 鎻愪緵涓�涓柟娉曟潵鍋滄绾跨▼
+ public void requestStop() {
+ isRunning = false;
+ }
+
}
--
Gitblit v1.9.1