From 0c5a58771fcf86ad0b562829fbfa440da9392703 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期三, 17 十二月 2025 09:08:16 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/impl/ZyStationThread.java |   79 +++++++++++++++++++++++----------------
 1 files changed, 46 insertions(+), 33 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/ZyStationThread.java b/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
index 6eb4b94..246f081 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
@@ -28,6 +28,10 @@
 
 import java.text.MessageFormat;
 import java.util.*;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
 
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
@@ -46,8 +50,8 @@
     private int deviceLogCollectTime = 200;
     private long deviceDataLogTime = System.currentTimeMillis();
     private volatile boolean closed = false;
-    private Thread mainThread;
-    private Thread readThread;
+    private ScheduledExecutorService readExecutor;
+    private ScheduledExecutorService processExecutor;
 
     public ZyStationThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
         this.deviceConfig = deviceConfig;
@@ -59,45 +63,54 @@
     public void run() {
         this.connect();
         deviceLogCollectTime = Utils.getDeviceLogCollectTime();
-        mainThread = Thread.currentThread();
 
-        readThread = new Thread(() -> {
-            while (!closed && !Thread.currentThread().isInterrupted()) {
-                try {
-                    readStatus();
-                    Thread.sleep(200);
-                } catch (InterruptedException ie) {
-                    Thread.currentThread().interrupt();
-                    break;
-                } catch (Exception e) {
-                    log.error("StationThread Fail", e);
-                }
+        readExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+            @Override
+            public Thread newThread(Runnable r) {
+                Thread t = new Thread(r);
+                t.setName("DevpReader-" + deviceConfig.getDeviceNo());
+                t.setDaemon(true);
+                return t;
             }
         });
-        readThread.start();
+        readExecutor.scheduleAtFixedRate(() -> {
+            if (closed || Thread.currentThread().isInterrupted()) {
+                return;
+            }
+            try {
+                deviceLogCollectTime = Utils.getDeviceLogCollectTime();
+                readStatus();
+            } catch (Exception e) {
+                log.error("StationThread Fail", e);
+            }
+        }, 0, 200, TimeUnit.MILLISECONDS);
 
-        while (!closed && !Thread.currentThread().isInterrupted()) {
+        processExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+            @Override
+            public Thread newThread(Runnable r) {
+                Thread t = new Thread(r);
+                t.setName("DevpWriter-" + deviceConfig.getDeviceNo());
+                t.setDaemon(true);
+                return t;
+            }
+        });
+        processExecutor.scheduleAtFixedRate(() -> {
+            if (closed || Thread.currentThread().isInterrupted()) {
+                return;
+            }
             try {
                 int step = 1;
                 Task task = MessageQueue.poll(SlaveType.Devp, deviceConfig.getDeviceNo());
                 if (task != null) {
                     step = task.getStep();
                 }
-                switch (step) {
-                    case 2:
-                        sendCommand((StationCommand) task.getData());
-                        break;
-                    default:
-                        break;
+                if (step == 2 && task != null) {
+                    sendCommand((StationCommand) task.getData());
                 }
-                Thread.sleep(200);
-            } catch (InterruptedException ie) {
-                Thread.currentThread().interrupt();
-                break;
             } catch (Exception e) {
                 e.printStackTrace();
             }
-        }
+        }, 0, 200, TimeUnit.MILLISECONDS);
     }
 
     private void readStatus() {
@@ -177,13 +190,13 @@
     @Override
     public void close() {
         closed = true;
-        Thread t = mainThread;
-        if (t != null) {
-            try { t.interrupt(); } catch (Exception ignore) {}
+        ScheduledExecutorService ex = readExecutor;
+        if (ex != null) {
+            try { ex.shutdownNow(); } catch (Exception ignore) {}
         }
-        Thread rt = readThread;
-        if (rt != null) {
-            try { rt.interrupt(); } catch (Exception ignore) {}
+        ScheduledExecutorService px = processExecutor;
+        if (px != null) {
+            try { px.shutdownNow(); } catch (Exception ignore) {}
         }
         if (zyStationConnectDriver != null) {
             zyStationConnectDriver.close();

--
Gitblit v1.9.1