对电池电压新增单线程执行,优化修该,节省线程开销

onereq
liuguijing 3 months ago
parent 3b492e7ea3
commit c09ed76819

@ -52,9 +52,6 @@ import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; 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.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -96,6 +93,7 @@ public class MpMasterService extends Service {
private int mPrevDateForLogs = 0; private int mPrevDateForLogs = 0;
private int mMasterTimers = 0; private int mMasterTimers = 0;
private SingletonThread batterySingleThread;
public static class STATE_SERVICE { public static class STATE_SERVICE {
public static final int CONNECTED = 10; public static final int CONNECTED = 10;
@ -337,7 +335,9 @@ public class MpMasterService extends Service {
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
if (batterySingleThread != null) {
batterySingleThread.shutdown();
}
super.onDestroy(); super.onDestroy();
} }
@ -1094,11 +1094,11 @@ public class MpMasterService extends Service {
} }
private void buildChargingBatteryVoltage(long ts) { private void buildChargingBatteryVoltage(long ts) {
SingletonThread instance = SingletonThread.getInstance(); batterySingleThread = SingletonThread.getInstance();
instance.executeTask(new Runnable() { batterySingleThread.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
logger.info("电压线程开始" ); logger.info("电压线程开始");
int val = 0; int val = 0;
for (int idx = 0; idx < 3; idx++) { for (int idx = 0; idx < 3; idx++) {
logger.info("电压测试第" + idx + "次开始读取"); logger.info("电压测试第" + idx + "次开始读取");

@ -1,47 +1,82 @@
package com.xypower.mpmaster; package com.xypower.mpmaster;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger; import java.util.logging.Logger;
public class SingletonThread { public class SingletonThread {
private static SingletonThread instance; private static volatile SingletonThread instance;
private Thread workerThread; private final ExecutorService executor;
private final AtomicBoolean isRunning = new AtomicBoolean(false); private final AtomicBoolean isRunning = new AtomicBoolean(false);
private final Object lock = new Object(); // 用于同步的锁对象 private final Object lock = new Object();
private SingletonThread() {}
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) { if (instance == null) {
instance = new SingletonThread(); synchronized (SingletonThread.class) {
if (instance == null) {
instance = new SingletonThread();
}
}
} }
return instance; return instance;
} }
public void executeTask(Runnable task) { /**
synchronized (lock) { // 加锁,确保检查+设置是一个原子操作 * 线
*
* @param task
* @return truefalse
*/
public boolean execute(Runnable task) {
if (task == null) {
throw new IllegalArgumentException("Task cannot be null");
}
synchronized (lock) {
if (isRunning.get()) { if (isRunning.get()) {
return; return false;
} }
isRunning.set(true); // 立即标记为运行中,防止其他线程进入 isRunning.set(true);
} }
workerThread = new Thread(() -> { executor.execute(() -> {
try { try {
task.run(); task.run();
} finally { } finally {
isRunning.set(false); // 任务完成后重置状态 synchronized (lock) {
isRunning.set(false);
}
} }
}); });
workerThread.start();
}
public boolean isThreadRunning() { return true;
return isRunning.get();
} }
public void waitForCompletion() throws InterruptedException { /**
if (workerThread != null) { *
workerThread.join(); */
public boolean isBusy() {
synchronized (lock) {
return isRunning.get();
} }
} }
/**
* 线退
*/
public void shutdown() {
executor.shutdown();
}
} }
Loading…
Cancel
Save