项目需要导入excel 表格数据,使用for循环4000条处理后数据约16分钟,修改未多线程后用时1-2分钟;
java实现多线程方式网上较多百度即可,本文只记录实现Callable;
jdk1.8增加 new ArrayList<>().parallelStream().forEach(); 并行处理for 循环 可以尝试,自用因系统原因报错故放弃。
1.创建类实现Callable
public class ImportCallable implements Callable {
private List<aaa> list; private CountDownLatch countDownLatch;
public JSONObject data;
public ImportCallable(List<aaa> list,CountDownLatch countDownLatch){ this.list = list; this.countDownLatch = countDownLatch; }
@Override public Object call() throws Exception { data = new JSONObject(); List<Integer> a1= new ArrayList<>(); List<Integer> a2= new ArrayList<>(); List<Integer> a3= new ArrayList<>(); if (null != list) { for (int i = 0; i < list.size(); i++) { //业务逻辑