多线程实现
private ArrayList<JSONObject> multiThreading(ArrayList list, List<String> errorMsg) {
int threadSize = 8;
int dataSize = list.size();
int threadNum = dataSize / threadSize + 1;
CountDownLatch countDownLatch = new CountDownLatch(threadNum);
boolean special = dataSize % threadSize == 0;
ExecutorService exec = Executors.newFixedThreadPool(threadNum);
List<Callable<Boolean>> tasks = Lists.newArrayList();
Callable<Boolean> task;
List loopDataList;
final ArrayList<JSONObject> finalResult = new ArrayList<>();
for (int i = 0; i < threadNum; i++) {
if (i == threadNum - 1) {
if (special) {
countDownLatch.countDown();
break;
}
loopDataList = list.subList(threadSize * i, dataSize);
} else {
loopDataList = list.subList(threadSize * i, threadSize * (i + 1));
}
final List recipientInfoDTOS = loopDataList;
task = new Callable<Boolean>() {
@Override
public Boolean call() {
for (Object recipientInfoDTO : recipientInfoDTOS) {
}
return true;
}
};
countDownLatch.countDown();
tasks.add(task);
}
try {
exec.invokeAll(tasks);
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
exec.shutdown();
return finalResult;
}