创建一个线程池(java)
Posted xiaowejie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建一个线程池(java)相关的知识,希望对你有一定的参考价值。
private ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("billService-pool-%d").build();
private final ExecutorService pool = new ThreadPoolExecutor(20, 20,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(512),threadFactory);
//解析实例:
final CountDownLatch countDownLatch = new CountDownLatch(billUploadHistory.size());//线程统计 for (BillUpload billUpload : billUploadHistory) { pool.execute(new Runnable() { @Override public void run() { PaybillParser parseService = null; if (EnumTypeOfInt.PAY_CHANNEL_WECHAT.getValue().equals(billUpload.getPayChannel())) { parseService = new WeixinPaybillParser(billUpload, logId); } else if (EnumTypeOfInt.PAY_CHANNEL_ALIPAY.getValue().equals(billUpload.getPayChannel())) { parseService = new AliPaybillParser(billUpload, logId); } if (parseService != null) { try { parseService.parse(); } catch (Exception e) { e.printStackTrace(); } finally { countDownLatch.countDown();//线程减1 } } } }); } try { countDownLatch.await();//阻塞直到计数器为0 } catch (InterruptedException e) { e.printStackTrace(); }
以上是关于创建一个线程池(java)的主要内容,如果未能解决你的问题,请参考以下文章