线程池的简单使用

//获取第三方数据接口demo
public void updatePlcParams(Set<String> stringSet) {
log.info(" === updatePlcParams() 更新plc数据开始 ========");
long start = System.currentTimeMillis();
/** 获取cpu核心数:*/
int nThreads = Runtime.getRuntime().availableProcessors();
log.debug(" === 启动线程数 =" + nThreads);
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(nThreads);
try {
for (String paramUrl : stringSet) {
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
// long start1 = System.currentTimeMillis();
String result = finishingPlcService.cachePut(paramUrl);
// long time1 = System.currentTimeMillis() - start1;
// log.debug(" >>>> "+paramUrl+" :"+result +" >>> time:"+time1);
}
});
}

} catch (Exception e) {
log.warn(e.getMessage(), e);
} finally {
fixedThreadPool.shutdown();//关闭

while (true) {
if (fixedThreadPool.isTerminated()) {
long time = System.currentTimeMillis() - start;
log.info("[" + DateConvertUtils.now() + "]:" + "=== updatePlcParams() 更新plc数据结束了,总耗时:" + time + " ms(毫秒)!\n");
break;
}
}
}
}
posted @ 2022-10-12 16:58  星空物语之韵  阅读(29)  评论(0)    收藏  举报