第一步:申明线程池 public ExecutorService taskExecutor = new ThreadPoolExecutor(6, Integer.MAX_VALUE, 0, TimeUnit.MILLISECONDS, new SynchronousQueue());
第二步:分批处理数据
// 每批次数量 Integer batchCount = 100;Integer start = 0; // 结尾 List list=new ArrayList<>(); Integer end = 100; int loopCount = (int) Math.ceil((float) list.size() / batchCount); // 采用线程池的同步计数类 CountDownLatch countDownLatch = new CountDownLatch(loopCount); for (int k = 0; k < loopCount; k++) { final Integer startTemp = start; final Integer endTemp = end; // 开启线程池 taskExecutor.execute(new Runnable() { @Override public void run() { try { //todo 书写业务代码逻辑 } catch (Exception ex) { LOGGER.error("getQwAllUserInfo error:", ex); } finally { countDownLatch.countDown(); } } }); //计算下次分片数据 start = end; end = start + batchCount > list.size() ? list.size() : batchCount + start; }
最后等所有线程数据处理完毕
countDownLatch.await();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)