调用方法
int threadNum = 7; while(true) { List<FaceAnalyseImage> list = faceAnalyseImageMapper.selectAllWaitDetectLimit2000(); if(list == null || list.size() == 0) { break; } if(list.size() <= threadNum) { threadNum = list.size(); } subList(list, threadNum); }
list按线程数量分割
StringBuffer ret = new StringBuffer(); int size = list.size(); ExecutorService executorService = Executors.newFixedThreadPool(nThreads); List<Future<String>> futures = new ArrayList<Future<String>>(nThreads); for (int i = 0; i < nThreads; i++) { final List<String> subList = list.subList(size / nThreads * i, size/ nThreads * (i + 1)); Callable<String> task = new Callable<String>() { public String call() throws Exception { getDataRunByDistinctTruckid(subList); return ""; } }; futures.add(executorService.submit(task)); } for (Future<String> future : futures) { ret.append(future.get()); } executorService.shutdown(); return ret.toString();