Browse Source

#定时任务线程优化

zhongwei
yang_shj 6 months ago
parent
commit
e71ee9a69f
  1. 20
      hzims-service/hzims-scheduled/src/main/java/com/hnac/hzims/scheduled/service/operation/monitor/impl/MonitorServiceImpl.java

20
hzims-service/hzims-scheduled/src/main/java/com/hnac/hzims/scheduled/service/operation/monitor/impl/MonitorServiceImpl.java

@ -78,7 +78,7 @@ public class MonitorServiceImpl implements MonitorService {
private final IAnalyseDataSearchClient analyseDataSearchClient;
private static final ExecutorService pool = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024), new ThreadFactoryBuilder().setNameFormat("load-monitoring-pool-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
private static final ExecutorService pool = new ThreadPoolExecutor(10, 10, 60L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(256), new ThreadFactoryBuilder().setNameFormat("load-monitoring-pool-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
@Value("${hzims.equipment.emInfo.emInfoList}")
@ -222,9 +222,12 @@ public class MonitorServiceImpl implements MonitorService {
});
countDownLatch.countDown();
}));
// 等待所有线程执行完成
try {
countDownLatch.await();
// 等待所有线程执行完成(至多等待三秒,超过三秒任务认为线程发生阻塞)
boolean flag = countDownLatch.await(3,TimeUnit.SECONDS);
if(!flag){
log.error("实时数据调度发生阻塞 : {}" , DateUtil.format(new Date(),DateUtil.PATTERN_DATETIME));
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
@ -299,6 +302,7 @@ public class MonitorServiceImpl implements MonitorService {
for(Map<String, List<StationAttributeEntity>> item : limit){
// 提交线程任务
pool.submit(()->{
try{
item.forEach((key,value)->{
RealStationVo realStation = new RealStationVo();
// 设备信息
@ -315,11 +319,19 @@ public class MonitorServiceImpl implements MonitorService {
realStations.add(realStation);
});
countDownLatch.countDown();
}catch (Exception e){
countDownLatch.countDown();
Thread.currentThread().interrupt();
}
});
}
// 等待所有线程执行完成
try {
countDownLatch.await();
// 等待所有线程执行完成(至多等待二秒,超过二秒任务认为线程发生阻塞)
boolean flag = countDownLatch.await(2,TimeUnit.SECONDS);
if(!flag){
log.error("集中监控数据调度发生阻塞 : {}" , DateUtil.format(new Date(),DateUtil.PATTERN_DATETIME));
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();

Loading…
Cancel
Save