// 并行xxx个线程直接进行送分 public void concurrentAddPoint(List<MppCumulativeDetails> detailsList,List<String> statList, int addSum) {// 初始化任务池 if (CollectionUtils.isEmpty(detailsList)) return; // 初始化任务 long time = System.currentTimeMillis(); int totalSize = detailsList.size(); int concurrentSize = (totalSize + addSum) / addSum; Executer exe = new Executer(concurrentSize); for (int i = 0; i < concurrentSize; i++) { List<MppCumulativeDetails> l = null; if (i < concurrentSize - 1) { l = detailsList.subList(i * addSum, (i + 1) * addSum); } else { l = detailsList.subList(i * addSum, detailsList.size()); } ConcurrentJob job = new ConcurrentJob(l,statList); exe.fork(job);// 派发任务 } // 汇总任务结果 int result = (Integer) exe.join(); logger.info("ConcurrentAdding result:"+result); logger.info("ConcurrentAdding Info: totalSize[ " + totalSize + " ], threadSize[ " + concurrentSize + " ], addSum[ " + addSum + " ]"); logger.info("ConcurrentAdding Cost Time: " + (System.currentTimeMillis() - time)); exe.shutdown(); } class ConcurrentJob extends Job { private List<MppCumulativeDetails> detailsList = new ArrayList<MppCumulativeDetails>(); private List<String> statList = new ArrayList<String>(); public ConcurrentJob(List<MppCumulativeDetails> detailsList,List<String> statList) { super(); this.detailsList = detailsList; this.statList = statList; } public Object execute() { long time = System.currentTimeMillis(); try { invokAddPointMethod(detailsList,statList); } catch (Exception e) { logger.error("ConcurrentAddPoint "+Thread.currentThread().getName() + "error!!!",e); return 0; } logger.info("ConcurrentAddPoint thread = " + Thread.currentThread().getName() + " | size: " + detailsList.size() + " | time: " + (System.currentTimeMillis() - time)); return 1; } } static class Executer { // 存储任务的执行结果 private List<Future<?>> futres = new ArrayList<Future<?>>(); // 条件队列锁,以及线程计数器 public final Lock lock = new Lock(); // 线程池 private ExecutorService pool = null; public Executer() { this(1); } public Executer(int threadPoolSize) { pool = Executors.newFixedThreadPool(threadPoolSize); } /** * 任务派发 * * @param job */ public void fork(Job job) { // 设置同步锁 job.setLock(lock); // 将任务派发给线程池去执行 futres.add(pool.submit(job)); // 增加线程数 synchronized (lock) { lock.thread_count++; } } /** * 统计任务结果 */ public int join() { synchronized (lock) { while (lock.thread_count > 0) {// 检查线程数,如果为0,则表示所有任务处理完成 // System.out.println("threadCount: "+THREAD_COUNT); try { lock.wait();// 如果任务没有全部完成,则挂起。等待完成的任务给予通知 } catch (InterruptedException e) { e.printStackTrace(); } } } int result = 0; // 取出每个任务的处理结果,汇总后返回 for (Future<?> future : futres) { try { Object o = future.get();// 因为任务都已经完成,这里直接get if (null != o) result += ((Integer) o); } catch (Exception e) { e.printStackTrace(); } } return result; } public void shutdown() { pool.shutdown(); futres = null; pool = null; } } static abstract class Job implements Callable<Object> { // 锁 private Lock lock = null; void setLock(Lock lock) { this.lock = lock; } public Object call() throws Exception { Object result = null; try { result = this.execute();// 执行子类具体任务 } catch (Exception e) { e.printStackTrace(); } synchronized (lock) { // 处理完业务后,任务结束,递减线程数,同时唤醒主线程 lock.thread_count--; lock.notifyAll(); } return result; } /** * 业务处理函数 */ public abstract Object execute(); } static class Lock { // 线程数 int thread_count; }