From c09ed768195a93a8c86c7dec83370e9dd7659c3a Mon Sep 17 00:00:00 2001 From: liuguijing <1440265357@qq.com> Date: Fri, 28 Mar 2025 09:38:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E7=94=B5=E6=B1=A0=E7=94=B5=E5=8E=8B?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8D=95=E7=BA=BF=E7=A8=8B=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BF=AE=E8=AF=A5=EF=BC=8C=E8=8A=82?= =?UTF-8?q?=E7=9C=81=E7=BA=BF=E7=A8=8B=E5=BC=80=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpmaster/MpMasterService.java | 14 ++-- .../com/xypower/mpmaster/SingletonThread.java | 73 ++++++++++++++----- 2 files changed, 61 insertions(+), 26 deletions(-) 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