线程池的简单使用
//获取第三方数据接口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;
}
}
}
}
我是个双鱼座的小王子,沉浸在自己的代码世界里,去探索这未知的世界,希望遇到更多的小伙伴一起前行!

浙公网安备 33010602011771号