diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index 42062e47..8985eb0f 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -52,9 +52,6 @@ import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; @@ -96,6 +93,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; @@ -337,7 +335,9 @@ public class MpMasterService extends Service { } catch (Exception ex) { } } - + if (batterySingleThread != null) { + batterySingleThread.shutdown(); + } super.onDestroy(); } @@ -1094,11 +1094,11 @@ public class MpMasterService extends Service { } private void buildChargingBatteryVoltage(long ts) { - SingletonThread instance = SingletonThread.getInstance(); - instance.executeTask(new Runnable() { + batterySingleThread = SingletonThread.getInstance(); + batterySingleThread.execute(new Runnable() { @Override public void run() { - logger.info("电压线程开始" ); + logger.info("电压线程开始"); int val = 0; for (int idx = 0; idx < 3; idx++) { logger.info("电压测试第" + idx + "次开始读取"); diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/SingletonThread.java b/mpmaster/src/main/java/com/xypower/mpmaster/SingletonThread.java index 01223046..91306590 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/SingletonThread.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/SingletonThread.java @@ -1,47 +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 SingletonThread instance; - private Thread workerThread; + private static volatile SingletonThread instance; + private final ExecutorService executor; private final AtomicBoolean isRunning = new AtomicBoolean(false); - private final Object lock = new Object(); // 用于同步的锁对象 - private SingletonThread() {} + private final Object lock = new Object(); - public static synchronized SingletonThread getInstance() { + 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) { - instance = new SingletonThread(); + synchronized (SingletonThread.class) { + if (instance == null) { + instance = new SingletonThread(); + } + } } return instance; } - public void executeTask(Runnable task) { - synchronized (lock) { // 加锁,确保检查+设置是一个原子操作 + /** + * 执行任务(线程安全且节省资源) + * + * @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; + return false; } - isRunning.set(true); // 立即标记为运行中,防止其他线程进入 + isRunning.set(true); } - workerThread = new Thread(() -> { + executor.execute(() -> { try { task.run(); } finally { - isRunning.set(false); // 任务完成后重置状态 + synchronized (lock) { + isRunning.set(false); + } } }); - workerThread.start(); - } - public boolean isThreadRunning() { - return isRunning.get(); + return true; } - public void waitForCompletion() throws InterruptedException { - if (workerThread != null) { - workerThread.join(); + /** + * 检查是否有任务正在执行 + */ + public boolean isBusy() { + synchronized (lock) { + return isRunning.get(); } } + + /** + * 关闭线程池(在应用退出时调用) + */ + public void shutdown() { + executor.shutdown(); + } } \ No newline at end of file