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

nx2024
liuguijing 3 months ago
parent 5dabcef8fe
commit e359260de7

@ -92,6 +92,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;
@ -330,7 +331,9 @@ public class MpMasterService extends Service {
} catch (Exception ex) { } catch (Exception ex) {
} }
} }
if (batterySingleThread != null) {
batterySingleThread.shutdown();
}
super.onDestroy(); super.onDestroy();
} }
@ -1087,23 +1090,29 @@ public class MpMasterService extends Service {
} }
private void buildChargingBatteryVoltage(long ts) { private void buildChargingBatteryVoltage(long ts) {
logger.info("电压测试开始"); batterySingleThread = SingletonThread.getInstance();
int val = 0; batterySingleThread.execute(new Runnable() {
for (int idx = 0; idx < 3; idx++) { @Override
logger.info("电压测试第" + idx + "次开始读取"); public void run() {
val = MpMasterService.getInt(112); logger.info("电压线程开始");
logger.info("电压测试第" + idx + "次读取结束 " + val); int val = 0;
if (val > 0) { for (int idx = 0; idx < 3; idx++) {
break; 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() { public String getAndResetMaxBCV() {

@ -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 truefalse
*/
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();
}
}
Loading…
Cancel
Save