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

onereq
liuguijing 3 months ago
parent 3b492e7ea3
commit c09ed76819

@ -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,8 +1094,8 @@ 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("电压线程开始");

@ -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();
private SingletonThread() {
// 使用单线程池,复用线程
executor = Executors.newSingleThreadExecutor(r -> {
Thread thread = new Thread(r, "OptimizedSingletonThread");
thread.setPriority(Thread.NORM_PRIORITY - 1); // 稍低优先级
return thread;
});
}
public static synchronized SingletonThread getInstance() {
// 双重检查锁定单例模式
public static SingletonThread getInstance() {
if (instance == null) {
synchronized (SingletonThread.class) {
if (instance == null) {
instance = new SingletonThread();
}
}
}
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()) {
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();
return true;
}
public boolean isThreadRunning() {
/**
*
*/
public boolean isBusy() {
synchronized (lock) {
return isRunning.get();
}
public void waitForCompletion() throws InterruptedException {
if (workerThread != null) {
workerThread.join();
}
/**
* 线退
*/
public void shutdown() {
executor.shutdown();
}
}
Loading…
Cancel
Save