今天做导出 查出的数据有3万条 还要遍历做一些处理 结果就超时
想到多线程遍历list 去网上查找 找到这块代码:
public String list2Str(List list, final int nThreads) throws Exception {
if (list == null || list.isEmpty()) {
return null;
}
StringBuffer ret = new StringBuffer();
int size = list.size();
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
List> futures = new ArrayList>(nThreads);
for (int i = 0; i < nThreads; i++) {
final List subList = list.subList(size / nThreads * i, size / nThreads * (i + 1));
Callable task = new Callable() {
@Override
public String call() throws Exception {
StringBuffer sb = new StringBuffer();
for (String str : subList) {
sb.append(str);
}
return sb.toString();
}
};
futures.add(executorService.submit(task));
}
for (Future future : futures) {
ret.append(future.get());
}
executorService.shutdown();
return ret.toString();
}
}
我调用时开了5个线程 我想问 每次导出就开启5个线程 如果多个人同时导出 比如10000个人同时导出 那岂不是就开启了50000个线程了!!!
没问题吗???