张世豪
2025-11-26 2b756769ea4adad21332d8a294871712cd42cc3f
src/chushihua/lunxun.java
@@ -5,8 +5,10 @@
import home.Fkj;
import home.MachineConfig;
import publicway.QueryData;
import publicway.TimestampUtil;
import xitongshezhi.SystemDebugDialog;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -16,22 +18,17 @@
 * 用于定时向所有卡槽发送查询指令
 * 支持暂停和恢复功能,检查串口连接状态
 * 新增:不同卡槽状态使用不同查询频率
 * 优化:内存管理和长时间运行稳定性
 */
public class lunxun {
   private static volatile boolean isRunning = false;
   private static volatile boolean isPaused = false;
   private static final AtomicBoolean shouldStop = new AtomicBoolean(false);
   private static Thread pollingThread;
   private static int pollingInterval = 100; // 默认轮询间隔
   private static int pollingInterval =50; // 默认轮询间隔
   public static boolean sendChaxunzhiling=true;//是否向串口发送查询指令
   public static boolean isSendChaxunzhiling() {
      return sendChaxunzhiling;
   }
   public static void setSendChaxunzhiling(boolean sendChaxunzhiling) {
      lunxun.sendChaxunzhiling = sendChaxunzhiling;
   }
   // 添加静态变量控制人脸检测
   public static boolean ishaveface = false; // 是否检测到人脸,默认没有人脸
   // 卡槽相关常量
   private static final int MIN_SLOT = 1;
@@ -44,6 +41,15 @@
   // 性能优化:查询指令缓存
   private static final Map<Integer, String> queryCommandCache = new ConcurrentHashMap<>();
   // 内存优化:缓存大小限制和清理机制
   private static final int MAX_CACHE_SIZE = 100;
   private static final long CACHE_CLEANUP_INTERVAL = 60000; // 1分钟清理一次
   private static long lastCleanupTime = 0;
   // 错误日志限流机制
   private static final Map<String, Long> lastErrorLogTime = new ConcurrentHashMap<>();
   private static final long ERROR_LOG_INTERVAL = 5000; // 相同错误5秒内只记录一次
   // 调试模式控制
   public static  boolean DEBUG_ENABLED = false;
@@ -53,6 +59,14 @@
   private static final int HAS_CARD_QUERY_INTERVAL = 10000; // 有卡卡槽查询间隔:10秒
   private static final Map<Integer, Long> lastQueryTimeMap = new ConcurrentHashMap<>(); // 记录每个卡槽的最后查询时间
   public static boolean isSendChaxunzhiling() {
      return sendChaxunzhiling;
   }
   public static void setSendChaxunzhiling(boolean sendChaxunzhiling) {
      lunxun.sendChaxunzhiling = sendChaxunzhiling;
   }
   /**
    * 检查串口连接状态 - 优化版本,添加重试机制
    * @return true-串口已连接, false-串口未连接
@@ -76,11 +90,11 @@
            }
            serialConnected = true;
         } else {
            Errlog.logOperation("串口连接失败 - 串口未打开");
            logErrorWithRateLimit("serial_connection_failed", "串口连接失败 - 串口未打开");
            serialConnected = false;
         }
      } catch (Exception e) {
         Errlog.logOperation("串口连接检查异常: " + e.getMessage());
         logErrorWithRateLimit("serial_connection_exception", "串口连接检查异常: " + e.getMessage());
         serialConnected = false;
      }
@@ -117,10 +131,13 @@
      // 启动前严格检查串口连接
      if (!checkSerialConnectionWithRetry()) {
         Errlog.logOperation("串口未连接,无法启动轮询查询");
         logErrorWithRateLimit("start_polling_serial_failed", "串口未连接,无法启动轮询查询");
         return false;
      }
      // 启动前先清理一次内存
      performCleanup();
      // 从配置中获取轮询间隔
      loadPollingIntervalFromConfig();
@@ -137,7 +154,7 @@
         pollingThread.start(); 
         return true;
      } catch (Exception e) {
         Errlog.logOperation("启动轮询查询线程时发生异常: " + e.getMessage());
         logErrorWithRateLimit("start_polling_thread_exception", "启动轮询查询线程时发生异常: " + e.getMessage());
         isRunning = false;
         shouldStop.set(true);
         return false;
@@ -174,15 +191,15 @@
            pollingThread.join(3000); // 等待3秒
            // 检查线程是否还在运行
            if (pollingThread.isAlive()) {
               Errlog.logOperation("轮询线程未在3秒内停止,标记为守护线程并忽略");
               logErrorWithRateLimit("polling_thread_stop_timeout", "轮询线程未在3秒内停止,标记为守护线程并忽略");
               // 不强制停止,而是确保它是守护线程
               pollingThread.setDaemon(true);
            }
         } catch (InterruptedException e) {
            Errlog.logOperation("停止轮询查询时被中断: " + e.getMessage());
            logErrorWithRateLimit("stop_polling_interrupted", "停止轮询查询时被中断: " + e.getMessage());
            Thread.currentThread().interrupt();
         } catch (Exception e) {
            Errlog.logOperation("停止轮询线程时发生异常: " + e.getMessage());
            logErrorWithRateLimit("stop_polling_exception", "停止轮询线程时发生异常: " + e.getMessage());
         } finally {
            pollingThread = null;
         }
@@ -234,7 +251,7 @@
      // 恢复前检查串口连接
      if (!checkSerialConnectionWithRetry()) {
         Errlog.logOperation("串口未连接,无法恢复轮询查询");
         logErrorWithRateLimit("resume_polling_serial_failed", "串口未连接,无法恢复轮询查询");
         return false;
      }
@@ -276,7 +293,7 @@
    */
   public static void setPollingInterval(int interval) {
      if (interval < 10) {
         Errlog.logOperation("轮询间隔不能小于10ms");
         logErrorWithRateLimit("polling_interval_too_small", "轮询间隔不能小于10ms");
         return;
      }
@@ -328,7 +345,7 @@
            //System.out.println("配置系统未初始化,使用默认轮询间隔: " + pollingInterval + "ms");
         }
      } catch (Exception e) {
         Errlog.logOperation("加载轮询间隔配置失败: " + e.getMessage());
         logErrorWithRateLimit("load_polling_interval_failed", "加载轮询间隔配置失败: " + e.getMessage());
         //System.out.println("使用默认轮询间隔: " + pollingInterval + "ms");
      }
   }
@@ -351,174 +368,195 @@
   /**
    * 轮询任务内部类 - 优化版本
    * 支持不同状态卡槽的不同查询频率
    * 优化:内存管理和重用对象
    */
   private static class PollingTask implements Runnable {
      private int currentIndex = 0; // 当前索引,用于遍历slotArray
      private int consecutiveFailures = 0; // 连续失败次数
      private static final int MAX_CONSECUTIVE_FAILURES = 5; // 最大连续失败次数
       private int currentIndex = 0; // 当前索引,用于遍历slotArray
       private int consecutiveFailures = 0; // 连续失败次数
       private static final int MAX_CONSECUTIVE_FAILURES = 5; // 最大连续失败次数
       private final StringBuilder debugBuilder = new StringBuilder(100); // 重用 StringBuilder
      @Override
      public void run() {
         //System.out.println("轮询查询线程开始运行");
       @Override
       public void run() {
           //System.out.println("轮询查询线程开始运行");
         while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
            try {
           while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
               try {
                   // 检查是否暂停
                   if (isPaused) {
                       synchronized (lunxun.class) {
                           while (isPaused && isRunning && !shouldStop.get()) {
                               lunxun.class.wait(1000); // 等待1秒或直到被唤醒
                           }
                       }
                       continue;
                   }
//             System.out.println("查询中.....线程");
               // 检查是否暂停
               if (isPaused) {
                  synchronized (lunxun.class) {
                     while (isPaused && isRunning && !shouldStop.get()) {
                        lunxun.class.wait(1000); // 等待1秒或直到被唤醒
                     }
                  }
                  continue;
               }
                   // 定期检查串口连接状态(每10次循环检查一次)
                   if (currentIndex % 10 == 0 && !checkSerialConnectionWithRetry()) {
                       logErrorWithRateLimit("serial_disconnected", "串口连接断开,暂停轮询");
                       pausePolling();
                       continue;
                   }
               // 定期检查串口连接状态(每10次循环检查一次)
               if (currentIndex % 10 == 0 && !checkSerialConnectionWithRetry()) {
                  Errlog.logOperation("串口连接断开,暂停轮询");
                  pausePolling();
                  continue;
               }
                   // 定期清理缓存(每100次循环清理一次)
                   if (currentIndex % 100 == 0) {
                       cleanupOldCache();
                   }
               // 获取卡槽数组
               Fkj[] slotArray = SlotManager.getSlotArray();
               if (slotArray == null || slotArray.length == 0) {
                  Errlog.logOperation("卡槽数组未初始化");
                  Thread.sleep(pollingInterval);
                  continue;
               }
                   // 获取卡槽数组
                   Fkj[] slotArray = SlotManager.getSlotArray();
                   if (slotArray == null || slotArray.length == 0) {
                       logErrorWithRateLimit("slot_array_not_initialized", "卡槽数组未初始化");
                       Thread.sleep(pollingInterval);
                       continue;
                   }
               // 新增:根据卡槽状态和查询频率决定是否发送查询
               boolean sentQuery = false;
               long currentTime = System.currentTimeMillis();
                   // 遍历所有卡槽
                   for (int i = 0; i < slotArray.length; i++) {
                       if (!isRunning || shouldStop.get()) {
                           break;
                       }
                       int slotIndex = (currentIndex + i) % slotArray.length;
                       Fkj slot = slotArray[slotIndex];
                       if (slot != null) {
                           String hasCard = slot.getHasCard();
                           int slotNumber = slotIndex + 1;
               // 遍历卡槽,寻找需要查询的卡槽
               for (int i = 0; i < slotArray.length && !sentQuery; i++) {
                  int slotIndex = (currentIndex + i) % slotArray.length;
                  Fkj slot = slotArray[slotIndex];
                  if (slot != null) {
                     String hasCard = slot.getHasCard();
                     int slotNumber = slotIndex + 1;
                     Long lastQueryTime = lastQueryTimeMap.get(slotNumber);
                           // 检查是否因为人脸检测需要跳过有卡卡槽
                           if (ishaveface && "1".equals(hasCard)) {
                               // 跳过有卡卡槽,立即继续下一个,不等待
                               if (DEBUG_ENABLED) {
                                   debugBuilder.setLength(0);
                                   debugBuilder.append("检测到人脸,跳过有卡卡槽 ").append(slotNumber).append(" 的查询\n");
                                   SystemDebugDialog.appendAsciiData(debugBuilder.toString());
                               }
                               continue; // 立即继续下一个卡槽,不等待
                           }
                     // 确定查询间隔:只有hasCard="1"的卡槽使用10秒间隔,其他情况(包括"-1")都使用100ms间隔
                     int queryInterval = "1".equals(hasCard) ? HAS_CARD_QUERY_INTERVAL : NO_CARD_QUERY_INTERVAL;
                           // 发送查询指令到当前卡槽
                           boolean sendSuccess = sendQueryToSlot(slotNumber);
                           if (sendSuccess) {
                               // 更新最后查询时间
                               lastQueryTimeMap.put(slotNumber, System.currentTimeMillis());
                               consecutiveFailures = 0;
                     // 检查是否达到查询时间
                     if (lastQueryTime == null || currentTime - lastQueryTime >= queryInterval) {
                        if (sendQueryToSlot(slotNumber)) {
                           // 更新最后查询时间
                           lastQueryTimeMap.put(slotNumber, currentTime);
                           sentQuery = true;
                           consecutiveFailures = 0;
                               if (DEBUG_ENABLED) {
                                   String status;
                                   if ("1".equals(hasCard)) {
                                       status = "有卡";
                                   } else if ("-1".equals(hasCard)) {
                                       status = "未知";
                                   } else {
                                       status = "无卡";
                                   }
                                   debugBuilder.setLength(0);
                                   debugBuilder.append("Slot ").append(slotNumber)
                                              .append(" (").append(status).append(") 查询成功\n");
                                   SystemDebugDialog.appendAsciiData(debugBuilder.toString());
                               }
                           } else {
                               consecutiveFailures++;
                               if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
                                   logErrorWithRateLimit("consecutive_failures", "lunxun连续失败次数过多,暂停轮询");
                                   pausePolling();
                                   break;
                               }
                           }
                           // 成功发送查询后,等待100ms再继续下一个查询
                           Thread.sleep(100);
                           // 发送查询指令后等待50ms,然后继续下一个卡槽
                           Thread.sleep(50);
                       }
                   }
                           if (DEBUG_ENABLED) {
                              String status;
                              if ("1".equals(hasCard)) {
                                 status = "有卡";
                              } else if ("-1".equals(hasCard)) {
                                 status = "未知";
                              } else {
                                 status = "无卡";
                              }
                              if (DEBUG_ENABLED) {
                                  StringBuilder debugMsg = new StringBuilder(50);
                                  debugMsg.append("Slot ").append(slotNumber)
                                          .append(" (").append(status).append(") 查询成功,间隔: ")
                                          .append(queryInterval).append("ms\n");
                                  SystemDebugDialog.appendAsciiData(debugMsg.toString());
                              }
                           }
                        } else {
                           consecutiveFailures++;
                           if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
                              Errlog.logOperation("lunxun连续失败次数过多,暂停轮询");
                              pausePolling();
                              break;
                           }
                        }
                     }
                  }
               }
                   // 更新当前索引
                   currentIndex = (currentIndex + 1) % slotArray.length;
               // 更新当前索引
               currentIndex = (currentIndex + 1) % slotArray.length;
               } catch (InterruptedException e) {
                   //System.out.println("轮询查询线程被中断");
                   Thread.currentThread().interrupt();
                   break;
               } catch (Exception e) {
                   logErrorWithRateLimit("polling_exception", "轮询查询过程中发生异常: " + e.getMessage());
                   consecutiveFailures++;
               // 如果没有发送查询,等待一段时间再继续
               if (!sentQuery) {
                  Thread.sleep(pollingInterval);
               }
                   // 发生异常时等待一段时间再继续
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException ie) {
                       Thread.currentThread().interrupt();
                       break;
                   }
               }
           }
            } catch (InterruptedException e) {
               //System.out.println("轮询查询线程被中断");
               Thread.currentThread().interrupt();
               break;
            } catch (Exception e) {
               Errlog.logOperation("轮询查询过程中发生异常: " + e.getMessage());
               consecutiveFailures++;
               // 发生异常时等待一段时间再继续
               try {
                  Thread.sleep(1000);
               } catch (InterruptedException ie) {
                  Thread.currentThread().interrupt();
                  break;
               }
            }
         }
         //System.out.println("轮询查询线程结束运行");
           //System.out.println("轮询查询线程结束运行");
       }
      // 添加获取和设置人脸检测状态的方法
      public static boolean isIshaveface() {
          return ishaveface;
      }
      public static void setIshaveface(boolean ishaveface) {
          lunxun.ishaveface = ishaveface;
          if (DEBUG_ENABLED) {
              if (ishaveface) {
                  SystemDebugDialog.appendAsciiData("检测到人脸,轮询将只查询无卡卡槽\n");
              } else {
                  SystemDebugDialog.appendAsciiData("未检测到人脸,轮询恢复正常查询模式\n");
              }
          }
      }
      /**
       * 向指定卡槽发送查询指令 - 优化版本
       * 使用缓存指令,优化调试输出
       */
      private boolean sendQueryToSlot(int slotNumber) {
         try {
            // 使用缓存的查询指令
            String queryCommand = getCachedQueryCommand(slotNumber);
//           System.out.println("指令是:"+queryCommand);
            if (DEBUG_ENABLED) {
               SystemDebugDialog.appendAsciiData("send to "+slotNumber+" queryCommand");
            }
      /**
        * 向指定卡槽发送查询指令 - 优化版本
        * 使用缓存指令,优化调试输出
        */
       private boolean sendQueryToSlot(int slotNumber) {
           try {
               // 使用缓存的查询指令
               String queryCommand = getCachedQueryCommand(slotNumber);
               System.out.println(slotNumber+"发送了指令是:"+queryCommand+"时间"+TimestampUtil.getTimestamp());
               if (DEBUG_ENABLED) {
                   SystemDebugDialog.appendAsciiData("send to "+slotNumber+" queryCommand");
               }
            if (queryCommand != null && !queryCommand.trim().isEmpty()) {
               // 发送到串口
               if (queryCommand != null && !queryCommand.trim().isEmpty()) {
                   // 发送到串口
                   if(sendChaxunzhiling) {
                       boolean sendResult = Sendmsg.sendMessage(queryCommand);
                       if (sendResult) {
                           return true;
                       } else {
                           if (DEBUG_ENABLED) {
                               SystemDebugDialog.appendAsciiData(slotNumber+" Send query command to card slot err");
                           }
                           // 发送失败可能是串口断开,更新连接状态
                           serialConnected = false;
                           return false;
                       }
                   } else {
                       return false;
                   }
               } else {
                   logErrorWithRateLimit("empty_query_command", "生成的查询指令为空,卡槽: " + slotNumber);
                   return false;
               }
               if(sendChaxunzhiling) {
                  boolean sendResult = Sendmsg.sendMessage(queryCommand);
                  if (sendResult) {
                     return true;
                  } else {
                     if (DEBUG_ENABLED) {
                        SystemDebugDialog.appendAsciiData(slotNumber+" Send query command to card slot err");
                     }
                     // 发送失败可能是串口断开,更新连接状态
                     serialConnected = false;
                     return false;
                  }
               }else {
                  return false;
               }
            } else {
               Errlog.logOperation("生成的查询指令为空,卡槽: " + slotNumber);
               return false;
            }
         } catch (Exception e) {
            Errlog.logOperation("发送查询指令到卡槽 " + slotNumber + " 时发生异常: " + e.getMessage());
            // 发生异常时更新串口连接状态
            serialConnected = false;
            return false;
         }
      }
           } catch (Exception e) {
               logErrorWithRateLimit("send_query_exception", "发送查询指令到卡槽 " + slotNumber + " 时发生异常: " + e.getMessage());
               // 发生异常时更新串口连接状态
               serialConnected = false;
               return false;
           }
       }
   }
   /**
@@ -528,13 +566,13 @@
    */
   public static boolean sendImmediateQuery(int slotNumber) {
      if (slotNumber < MIN_SLOT || slotNumber > MAX_SLOT) {
         Errlog.logOperation("卡槽编号必须在" + MIN_SLOT + "-" + MAX_SLOT + "之间");
         logErrorWithRateLimit("invalid_slot_number", "卡槽编号必须在" + MIN_SLOT + "-" + MAX_SLOT + "之间");
         return false;
      }
      // 检查串口连接
      if (!checkSerialConnectionWithRetry()) {
         Errlog.logOperation("串口未连接,无法发送查询指令");
         logErrorWithRateLimit("immediate_query_serial_failed", "串口未连接,无法发送查询指令");
         return false;
      }
@@ -555,16 +593,16 @@
               }
               return true;
            } else {
               Errlog.logOperation("立即查询失败 - 发送指令到卡槽 " + slotNumber + " 失败");
               logErrorWithRateLimit("immediate_query_send_failed", "立即查询失败 - 发送指令到卡槽 " + slotNumber + " 失败");
               return false;
            }
         } else {
            Errlog.logOperation("立即查询失败 - 生成的查询指令为空,卡槽: " + slotNumber);
            logErrorWithRateLimit("immediate_query_empty_command", "立即查询失败 - 生成的查询指令为空,卡槽: " + slotNumber);
            return false;
         }
      } catch (Exception e) {
         Errlog.logOperation("立即查询卡槽 " + slotNumber + " 时发生异常: " + e.getMessage());
         logErrorWithRateLimit("immediate_query_exception", "立即查询卡槽 " + slotNumber + " 时发生异常: " + e.getMessage());
         return false;
      }
   }
@@ -576,7 +614,7 @@
   public static int sendImmediateQueryToAll() {
      // 检查串口连接
      if (!checkSerialConnectionWithRetry()) {
         Errlog.logOperation("串口未连接,无法发送批量查询指令");
         logErrorWithRateLimit("batch_query_serial_failed", "串口未连接,无法发送批量查询指令");
         return 0;
      }
@@ -629,7 +667,7 @@
      if (connected) {
         //            //System.out.println("串口连接状态已设置为: 已连接");
      } else {
         Errlog.logOperation("串口连接状态已设置为: 未连接");
         logErrorWithRateLimit("serial_disconnected_external", "串口连接状态已设置为: 未连接");
         // 如果串口断开且轮询正在运行,自动暂停轮询
         if (isRunning && !isPaused) {
            pausePolling();
@@ -668,9 +706,9 @@
         }
      }
      return String.format("轮询状态: %s, 串口: %s, 间隔: %dms, 指令缓存: %d, 卡槽范围: %d-%d, 无卡: %d(100ms), 有卡: %d(10s)",
      return String.format("轮询状态: %s, 串口: %s, 间隔: %dms, 指令缓存: %d, 卡槽范围: %d-%d, 无卡: %d(100ms), 有卡: %d(10s)\n%s",
            status, serialStatus, pollingInterval, cacheSize, MIN_SLOT, MAX_SLOT, 
            noCardCount, hasCardCount);
            noCardCount, hasCardCount, getMemoryStatus());
   }
   /**
@@ -699,7 +737,7 @@
         if (isPaused) {
            // 恢复前检查串口连接
            if (!checkSerialConnectionWithRetry()) {
               Errlog.logOperation("串口未连接,无法恢复轮询查询");
               logErrorWithRateLimit("external_resume_serial_failed", "串口未连接,无法恢复轮询查询");
               return false;
            }
@@ -756,7 +794,7 @@
    */
   public static void setNoCardQueryInterval(int interval) {
      if (interval < 10) {
         Errlog.logOperation("无卡卡槽查询间隔不能小于10ms");
         logErrorWithRateLimit("no_card_interval_too_small", "无卡卡槽查询间隔不能小于10ms");
         return;
      }
      // 注意:这里只是设置常量,实际运行时需要重新启动轮询才能生效
@@ -769,7 +807,7 @@
    */
   public static void setHasCardQueryInterval(int interval) {
      if (interval < 1000) {
         Errlog.logOperation("有卡卡槽查询间隔不能小于1000ms");
         logErrorWithRateLimit("has_card_interval_too_small", "有卡卡槽查询间隔不能小于1000ms");
         return;
      }
      // 注意:这里只是设置常量,实际运行时需要重新启动轮询才能生效
@@ -783,4 +821,102 @@
   public static void setDEBUG_ENABLED(boolean dEBUG_ENABLED) {
      DEBUG_ENABLED = dEBUG_ENABLED;
   }
   // ==================== 新增内存优化方法 ====================
   /**
    * 清理旧缓存 - 防止内存无限增长
    */
   private static void cleanupOldCache() {
      long currentTime = System.currentTimeMillis();
      if (currentTime - lastCleanupTime < CACHE_CLEANUP_INTERVAL) {
         return;
      }
      lastCleanupTime = currentTime;
      // 清理长时间未使用的查询时间记录
      long cleanupThreshold = currentTime - 300000; // 5分钟未使用
      lastQueryTimeMap.entrySet().removeIf(entry ->
         currentTime - entry.getValue() > cleanupThreshold
      );
      // 限制查询指令缓存大小
      if (queryCommandCache.size() > MAX_CACHE_SIZE) {
         Iterator<Map.Entry<Integer, String>> iterator = queryCommandCache.entrySet().iterator();
         int itemsToRemove = queryCommandCache.size() - MAX_CACHE_SIZE;
         for (int i = 0; i < itemsToRemove && iterator.hasNext(); i++) {
            iterator.next();
            iterator.remove();
         }
      }
      // 清理错误日志限流记录
      lastErrorLogTime.entrySet().removeIf(entry ->
         currentTime - entry.getValue() > 300000 // 5分钟
      );
   }
   /**
    * 限流错误日志 - 防止大量重复日志占用内存
    */
   private static void logErrorWithRateLimit(String errorKey, String message) {
      long currentTime = System.currentTimeMillis();
      Long lastTime = lastErrorLogTime.get(errorKey);
      if (lastTime == null || currentTime - lastTime > ERROR_LOG_INTERVAL) {
         Errlog.logOperation(message);
         lastErrorLogTime.put(errorKey, currentTime);
         // 清理过期的错误记录
         if (lastErrorLogTime.size() > 50) {
            lastErrorLogTime.entrySet().removeIf(entry ->
               currentTime - entry.getValue() > 300000 // 5分钟
            );
         }
      }
   }
   /**
    * 获取内存状态信息
    */
   public static String getMemoryStatus() {
      Runtime runtime = Runtime.getRuntime();
      long totalMemory = runtime.totalMemory();
      long freeMemory = runtime.freeMemory();
      long usedMemory = totalMemory - freeMemory;
      long maxMemory = runtime.maxMemory();
      return String.format("内存使用: %.2fMB/%.2fMB (最大: %.2fMB), 缓存: 时间记录=%d, 指令缓存=%d, 错误记录=%d",
         usedMemory / (1024.0 * 1024.0),
         totalMemory / (1024.0 * 1024.0),
         maxMemory / (1024.0 * 1024.0),
         lastQueryTimeMap.size(),
         queryCommandCache.size(),
         lastErrorLogTime.size());
   }
   /**
    * 手动触发内存清理
    */
   public static void performCleanup() {
      // 清理查询时间记录中长时间未查询的卡槽
      long cleanupThreshold = System.currentTimeMillis() - 3600000; // 1小时
      lastQueryTimeMap.entrySet().removeIf(entry ->
         entry.getValue() < cleanupThreshold
      );
      // 清空查询指令缓存
      queryCommandCache.clear();
      // 清空错误日志限流记录
      lastErrorLogTime.clear();
      // 建议系统进行垃圾回收(但不强制)
      System.gc();
      if (DEBUG_ENABLED) {
         SystemDebugDialog.appendAsciiData("执行内存清理完成\n");
      }
   }
}