对电池电压新增单线程执行,优化修改

onereq
liuguijing 3 months ago
parent bd7d4132e2
commit 3b492e7ea3

@ -150,27 +150,6 @@ public class MpMasterService extends Service {
//用于创建单例线程保证该线程在项目中唯一 //用于创建单例线程保证该线程在项目中唯一
private static final AtomicBoolean created = new AtomicBoolean(false); private static final AtomicBoolean created = new AtomicBoolean(false);
// private static Thread batteryVoltageThread;
// public static Thread getInstance(Runnable runnable) {
// if (!created.getAndSet(true)) {
// batteryVoltageThread = new Thread(runnable);
// batteryVoltageThread.start();
// }
// return batteryVoltageThread;
// }
private static ExecutorService batteryVoltageThread;
public static synchronized ExecutorService getSingleThreadExecutor() {
if (batteryVoltageThread == null || batteryVoltageThread.isShutdown()) {
batteryVoltageThread = Executors.newSingleThreadExecutor();
}
return batteryVoltageThread;
}
AtomicInteger reqCode = new AtomicInteger(0); AtomicInteger reqCode = new AtomicInteger(0);
public MpMasterService() { public MpMasterService() {
@ -298,6 +277,7 @@ public class MpMasterService extends Service {
IntentFilter intentFilter; IntentFilter intentFilter;
intentFilter = new IntentFilter(SimUtil.SMS_SEND_ACTION); intentFilter = new IntentFilter(SimUtil.SMS_SEND_ACTION);
registerReceiver(mSmsSnedReceiver, intentFilter); registerReceiver(mSmsSnedReceiver, intentFilter);
} }
public String getIccid(int number) { public String getIccid(int number) {
@ -1114,19 +1094,11 @@ public class MpMasterService extends Service {
} }
private void buildChargingBatteryVoltage(long ts) { private void buildChargingBatteryVoltage(long ts) {
if (batteryVoltageThread != null && !batteryVoltageThread.isShutdown()) { SingletonThread instance = SingletonThread.getInstance();
logger.info("电压线程在执行退出"); instance.executeTask(new Runnable() {
return;
}
getSingleThreadExecutor().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
try { logger.info("电压线程开始" );
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
logger.info("电压线程编号" + batteryVoltageThread.hashCode());
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 + "次开始读取");

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