diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index a54ec6df..c528c5c1 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -92,6 +92,7 @@ public class MpMasterService extends Service { private int mPrevDateForLogs = 0; private int mMasterTimers = 0; + private SingletonThread batterySingleThread; public static class STATE_SERVICE { public static final int CONNECTED = 10; @@ -330,7 +331,9 @@ public class MpMasterService extends Service { } catch (Exception ex) { } } - + if (batterySingleThread != null) { + batterySingleThread.shutdown(); + } super.onDestroy(); } @@ -1087,23 +1090,29 @@ public class MpMasterService extends Service { } private void buildChargingBatteryVoltage(long ts) { - logger.info("电压测试开始"); - int val = 0; - for (int idx = 0; idx < 3; idx++) { - logger.info("电压测试第" + idx + "次开始读取"); - val = MpMasterService.getInt(112); - logger.info("电压测试第" + idx + "次读取结束 " + val); - if (val > 0) { - break; + batterySingleThread = SingletonThread.getInstance(); + batterySingleThread.execute(new Runnable() { + @Override + public void run() { + logger.info("电压线程开始"); + int val = 0; + for (int idx = 0; idx < 3; idx++) { + logger.info("电压测试第" + idx + "次开始读取"); + val = MpMasterService.getInt(112); + logger.info("电压测试第" + idx + "次读取结束 " + val); + if (val > 0) { + break; + } + } + if (val > 0) { + if (val > mMaxBCV) { + mMaxBCV = val; + mMaxBCVTime = ts; + } + } } - } + }); - if (val > 0) { - if (val > mMaxBCV) { - mMaxBCV = val; - mMaxBCVTime = ts; - } - } } public String getAndResetMaxBCV() { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/SingletonThread.java b/mpmaster/src/main/java/com/xypower/mpmaster/SingletonThread.java new file mode 100644 index 00000000..91306590 --- /dev/null +++ b/mpmaster/src/main/java/com/xypower/mpmaster/SingletonThread.java @@ -0,0 +1,82 @@ +package com.xypower.mpmaster; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.Logger; + + +public class SingletonThread { + private static volatile SingletonThread instance; + private final ExecutorService executor; + private final AtomicBoolean isRunning = new AtomicBoolean(false); + private final Object lock = new Object(); + + private SingletonThread() { + // 使用单线程池,复用线程 + executor = Executors.newSingleThreadExecutor(r -> { + Thread thread = new Thread(r, "OptimizedSingletonThread"); + thread.setPriority(Thread.NORM_PRIORITY - 1); // 稍低优先级 + return thread; + }); + } + + // 双重检查锁定单例模式 + public static SingletonThread getInstance() { + if (instance == null) { + synchronized (SingletonThread.class) { + if (instance == null) { + instance = new SingletonThread(); + } + } + } + return instance; + } + + /** + * 执行任务(线程安全且节省资源) + * + * @param task 要执行的任务 + * @return true表示任务已接受执行,false表示跳过执行 + */ + public boolean execute(Runnable task) { + if (task == null) { + throw new IllegalArgumentException("Task cannot be null"); + } + + synchronized (lock) { + if (isRunning.get()) { + return false; + } + isRunning.set(true); + } + + executor.execute(() -> { + try { + task.run(); + } finally { + synchronized (lock) { + isRunning.set(false); + } + } + }); + + return true; + } + + /** + * 检查是否有任务正在执行 + */ + public boolean isBusy() { + synchronized (lock) { + return isRunning.get(); + } + } + + /** + * 关闭线程池(在应用退出时调用) + */ + public void shutdown() { + executor.shutdown(); + } +} \ No newline at end of file