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/ZySiemensCrnThread.java |   70 ++++++++++++++++++++++++----------
 1 files changed, 49 insertions(+), 21 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java b/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
index d8decf5..a29d75a 100644
--- a/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
@@ -12,7 +12,6 @@
 import com.zy.asrs.service.BasCrnpOptService;
 import com.zy.asrs.utils.Utils;
 import com.zy.common.utils.RedisUtil;
-import com.zy.core.News;
 import com.zy.core.cache.MessageQueue;
 import com.zy.core.cache.OutputQueue;
 import com.zy.core.enums.CrnTaskModeType;
@@ -31,6 +30,10 @@
 
 import java.text.MessageFormat;
 import java.util.Date;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 鍫嗗灈鏈虹嚎绋�
@@ -46,7 +49,8 @@
     private int deviceLogCollectTime = 200;
     private boolean resetFlag = false;
     private volatile boolean closed = false;
-    private Thread mainThread;
+    private ScheduledExecutorService readExecutor;
+    private ScheduledExecutorService processExecutor;
 
     public ZySiemensCrnThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
         this.deviceConfig = deviceConfig;
@@ -58,33 +62,53 @@
     public void run() {
         this.connect();
         this.initCrn();
-        mainThread = Thread.currentThread();
-        while (!closed && !Thread.currentThread().isInterrupted()) {
+        readExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+            @Override
+            public Thread newThread(Runnable r) {
+                Thread t = new Thread(r);
+                t.setName("CrnReader-" + deviceConfig.getDeviceNo());
+                t.setDaemon(true);
+                return t;
+            }
+        });
+        readExecutor.scheduleAtFixedRate(() -> {
+            if (closed || Thread.currentThread().isInterrupted()) {
+                return;
+            }
             try {
                 deviceLogCollectTime = Utils.getDeviceLogCollectTime();
+                readStatus();
+            } catch (Exception e) {
+                log.error("CrnThread Fail", e);
+            }
+        }, 0, 200, TimeUnit.MILLISECONDS);
+
+        processExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+            @Override
+            public Thread newThread(Runnable r) {
+                Thread t = new Thread(r);
+                t.setName("CrnWriter-" + deviceConfig.getDeviceNo());
+                t.setDaemon(true);
+                return t;
+            }
+        });
+        processExecutor.scheduleAtFixedRate(() -> {
+            if (closed || Thread.currentThread().isInterrupted()) {
+                return;
+            }
+            try {
                 int step = 1;
                 Task task = MessageQueue.poll(SlaveType.Crn, deviceConfig.getDeviceNo());
                 if (task != null) {
                     step = task.getStep();
                 }
-                switch (step) {
-                    case 1:
-                        readStatus();
-                        break;
-                    case 2:
-                        sendCommand((CrnCommand) task.getData());
-                        break;
-                    default:
-                        break;
+                if (step == 2 && task != null) {
+                    sendCommand((CrnCommand) task.getData());
                 }
-                Thread.sleep(200);
-            } catch (InterruptedException ie) {
-                Thread.currentThread().interrupt();
-                break;
             } catch (Exception e) {
                 e.printStackTrace();
             }
-        }
+        }, 0, 200, TimeUnit.MILLISECONDS);
     }
 
     /**
@@ -217,9 +241,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) {}
+        }
+        ScheduledExecutorService px = processExecutor;
+        if (px != null) {
+            try { px.shutdownNow(); } catch (Exception ignore) {}
         }
         if (zyCrnConnectDriver != null) {
             zyCrnConnectDriver.close();

--
Gitblit v1.9.1