fix:线程阻塞问题处理
This commit is contained in:
@@ -13,6 +13,7 @@ import org.springframework.core.annotation.Order;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
@@ -128,7 +129,38 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable {
|
|||||||
this.runs.put(deviceDriver.getDeviceCode(), runnable);
|
this.runs.put(deviceDriver.getDeviceCode(), runnable);
|
||||||
}
|
}
|
||||||
runnable.setIndex(this.runs);
|
runnable.setIndex(this.runs);
|
||||||
this.executorService.execute(runnable);
|
// this.executorService.execute(runnable);
|
||||||
|
Future<?> future = this.executorService.submit(runnable);
|
||||||
|
/* try {
|
||||||
|
// 设置超时时间为 30 秒,如果任务超时,抛出 TimeoutException
|
||||||
|
future.get(5, TimeUnit.SECONDS);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
System.out.println("任务超时,取消任务!");
|
||||||
|
// 超时后取消任务,并设置为 true 以中断执行中的线程
|
||||||
|
future.cancel(true);
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
//plc断连后强制终止线程
|
||||||
|
try {
|
||||||
|
// 设置超时时间为 15 秒,如果任务超时,抛出 TimeoutException
|
||||||
|
future.get(15, TimeUnit.SECONDS);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
System.out.println("任务超时,取消任务!");
|
||||||
|
// 使用反射获取 FutureTask 中的线程,并使用 Thread.stop() 强制终止
|
||||||
|
Field runnerField = future.getClass().getDeclaredField("runner");
|
||||||
|
runnerField.setAccessible(true);
|
||||||
|
Thread runnerThread = (Thread) runnerField.get(future);
|
||||||
|
if (runnerThread != null) {
|
||||||
|
runnerThread.stop(); // 强制终止线程(不推荐)
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package org.nl.acs.opc;
|
package org.nl.acs.opc;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.date.TimeInterval;
|
||||||
import cn.hutool.core.util.NumberUtil;
|
import cn.hutool.core.util.NumberUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.jinterop.dcom.common.JIException;
|
import org.jinterop.dcom.common.JIException;
|
||||||
|
|||||||
Reference in New Issue
Block a user