fix:线程阻塞问题处理3

This commit is contained in:
2024-08-16 14:50:41 +08:00
parent 65ffa06c5b
commit c8e93afd42
8 changed files with 37 additions and 90 deletions

View File

@@ -1,6 +1,7 @@
package org.nl.acs.agv;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
@@ -41,6 +42,7 @@ public class QueryAGVStatus {
private DeviceAppService deviceAppService;
public void run() {
log.info("定时查询AGV状态"+ DateUtil.now());
InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class);
TaskService taskService = SpringContextHolder.getBean(TaskService.class);
ISysParamService paramService = SpringContextHolder.getBean(ISysParamService.class);

View File

@@ -2,22 +2,18 @@ package org.nl.acs.opc;
import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.core.DtpRegistry;
import org.dromara.dynamictp.core.executor.DtpExecutor;
import org.dromara.dynamictp.core.support.ExecutorWrapper;
import org.nl.acs.auto.run.AbstractAutoRunnable;
import org.nl.acs.auto.run.AutoRunService;
import org.nl.acs.device_driver.driver.ExecutableDeviceDriver;
import org.nl.acs.instruction.service.InstructionService;
import org.nl.acs.udw.UnifiedDataAccessor;
import org.nl.acs.udw.UnifiedDataAccessorFactory;
import org.nl.config.SpringContextHolder;
import org.nl.config.thread.TheadFactoryName;
import org.nl.config.thread.ThreadPoolExecutorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.*;
@@ -38,10 +34,7 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable {
int loop_time_millions = 100;
@Resource
private ThreadPoolExecutor executorService;
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(30);
Map<String, BlockedRunable> runs;
@@ -121,33 +114,8 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable {
BlockedRunable runnable = new BlockedRunable() {
@Override
public void subRun() throws Exception {
// Thread.sleep(6000);
deviceDriver.executeAuto();
/* ThreadPoolExecutor executor= DtpRegistry.getDtpExecutor("dtpExecutor1");
Future<?> submit = executor.submit(() -> {
try {
deviceDriver.executeAuto();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
try {
// 设置超时时间为 15 秒,如果任务超时,抛出 TimeoutException
submit.get(10, TimeUnit.SECONDS);
System.out.println();
} catch (TimeoutException e) {
System.out.println("任务超时,取消任务!");
// 使用反射获取 FutureTask 中的线程,并使用 Thread.stop() 强制终止
Field runnerField = submit.getClass().getDeclaredField("runner");
runnerField.setAccessible(true);
Thread runnerThread = (Thread) runnerField.get(submit);
if (runnerThread != null) {
runnerThread.stop(); // 强制终止线程(不推荐)
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}*/
}
@Override
@@ -159,55 +127,16 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable {
this.runs.put(deviceDriver.getDeviceCode(), runnable);
}
runnable.setIndex(this.runs);
// 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();
}*/
ExecutorService executorService = (ExecutorService) ThreadPoolExecutorUtil.getPoll();
Future<?> future = executorService.submit(runnable);
//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(); // 强制终止线程(不推荐)
}
ScheduledExecutorService scheduler= (ScheduledExecutorService) DtpRegistry.getExecutor("scheduled_pool");
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}*/
// 使用调度器在超时后取消任务
// 使用调度器在超时后取消任务
scheduler.schedule(() -> {
if (!future.isDone()) {
// future.cancel(true); // 尝试中断任务
// Field runnerField = null;
// try {
// runnerField = future.getClass().getDeclaredField("runner");
// runnerField.setAccessible(true);
// Thread runnerThread = (Thread) runnerField.get(future);
// if (runnerThread != null) {
// runnerThread.stop(); // 强制终止线程(不推荐)
// }
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
try {
// 设置超时时间为 30 秒,如果任务超时,抛出 TimeoutException
future.get(5, TimeUnit.SECONDS);
@@ -218,15 +147,8 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable {
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}, 5, TimeUnit.SECONDS); // 设置超时时间为10秒
}, 1, TimeUnit.MILLISECONDS); // 设置超时时间为10秒
}
}
}

View File

@@ -52,6 +52,7 @@ public class OpcUtl {
e1.printStackTrace();
}
boolean is_success = true;
StringBuilder message = new StringBuilder();
Iterator arg4 = e.keySet().iterator();

View File

@@ -35,7 +35,7 @@ public class ThreadPoolExecutorUtil {
public static Executor getPoll(){
return DtpRegistry.getExecutor("dtpExecutor1");
return DtpRegistry.getExecutor("cool_pool");
/* AsyncTaskProperties properties = SpringContextHolder.getBean(AsyncTaskProperties.class);
return new ThreadPoolExecutor(

View File

@@ -46,6 +46,7 @@ public class AutoCreateInst {
* 创建指令前需要判断是否条件具备:起始位置是否有货、目标位置是否有货
*/
public void run() throws Exception {
log.info("自动生成指令"+DateUtil.now());
TaskService taskserver = SpringContextHolder.getBean(TaskService.class);
InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class);
RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class);

View File

@@ -43,6 +43,7 @@ public class CreateDDJInst {
* 定时查询堆垛机任务
*/
public void run() {
log.info("定时查询堆垛机任务"+DateUtil.now());
TaskService taskserver = SpringContextHolder.getBean(TaskService.class);
InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class);
RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class);

View File

@@ -1,5 +1,6 @@
package org.nl.system.service.quartz.task;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.auto.run.AutoRunService;
import org.nl.system.service.param.ISysParamService;
@@ -20,6 +21,7 @@ public class NdcAutoReconnection {
AutoRunService autoRunService;
public void run(String threadCode) throws Exception {
log.info("定时NDC自动重连"+ DateUtil.now());
String[] threadCodes = threadCode.split(",");
for (String code : threadCodes) {
if (!autoRunService.getThreadByCode(code).isAlive()) {

View File

@@ -57,7 +57,7 @@ spring:
runTimeout: 10000
queueTimeout: 100
executors: # 动态线程池配置,都有默认值,采用默认值的可以不配置该项,减少配置量
- threadPoolName: dtpExecutor1 # 线程池名称,必填
- threadPoolName: cool_pool # 线程池名称,必填dtpExecutor1
threadPoolAliasName: core_thread # 线程池别名,可选
executorType: common # 线程池类型 common、eager、ordered、scheduled、priority默认 common
corePoolSize: 30 # 核心线程数默认1
@@ -67,6 +67,24 @@ spring:
rejectedHandlerType: CallerRunsPolicy # 拒绝策略查看RejectedTypeEnum枚举类默认AbortPolicy
keepAliveTime: 30 # 空闲线程等待超时时间默认60
threadNamePrefix: core_thread # 线程名前缀默认dtp
tryInterrupt: true
allowCoreThreadTimeOut: true # 是否允许核心线程池超时默认false
waitForTasksToCompleteOnShutdown: true # 参考spring线程池设计优雅关闭线程池默认true
awaitTerminationSeconds: 5 # 优雅关闭线程池时阻塞等待线程池中任务执行时间默认3单位s
preStartAllCoreThreads: false # 是否预热所有核心线程默认false
runTimeout: 2000 # 任务执行超时阈值单位ms默认0不统计
queueTimeout: 1000 # 任务在队列等待超时阈值单位ms默认0不统计
- threadPoolName: scheduled_pool # 线程池名称,必填
threadPoolAliasName: scheduled_thread # 线程池别名,可选
executorType: scheduled # 线程池类型 common、eager、ordered、scheduled、priority默认 common
corePoolSize: 30 # 核心线程数默认1
maximumPoolSize: 50 # 最大线程数默认cpu核数
queueCapacity: 1024 # 队列容量默认1024
queueType: VariableLinkedBlockingQueue # 任务队列查看源码QueueTypeEnum枚举类默认VariableLinkedBlockingQueue
rejectedHandlerType: CallerRunsPolicy # 拒绝策略查看RejectedTypeEnum枚举类默认AbortPolicy
keepAliveTime: 30 # 空闲线程等待超时时间默认60
threadNamePrefix: scheduled_thread # 线程名前缀默认dtp
tryInterrupt: true
allowCoreThreadTimeOut: true # 是否允许核心线程池超时默认false
waitForTasksToCompleteOnShutdown: true # 参考spring线程池设计优雅关闭线程池默认true
awaitTerminationSeconds: 5 # 优雅关闭线程池时阻塞等待线程池中任务执行时间默认3单位s
@@ -142,4 +160,4 @@ lucene:
path: C:\acs\lucene\indexl
tlog:
pattern: "[$preApp][$preIp][$spanId][$traceId]"
enable-invoke-time-print: true
enable-invoke-time-print: true