线程池CompletableFuture allOf().join多线程阻塞直至所有线程结束

ExecutorService pool = Executors.newFixedThreadPool(1000);
CompletableFuture[] futureList = list.parallelStream().map(item-> CompletableFuture.supplyAsync(() -> {
            for(int i=0;i<24;i++){
                for(int j=0;j device_info = new ArrayList<>();

                    Timestamp upload_time = new Timestamp(item + i*3600000 + (long) (Math.random()*57*60*1000));
                    Timestamp gather_time = upload_time;
                    DeviceProtocol deviceProtocol = new DeviceProtocol();
                    deviceProtocol.setTenant_id(tenant_id);
                    deviceProtocol.setServer_id(server_id);
                    deviceProtocol.setUpload_time(upload_time);
                    deviceProtocol.setPrj_id(prj_id);
                    for (String device_id : getDevices()) {
                        ElectricModel model=new ElectricModel();
                        model.setDevice_id(device_id);
                        model.setGather_time(gather_time);
                        model.setDevice_path(device_path);
                        model.setState(0);
                        model.setElectric_meter(createElectricMeter(device_id,gather_time.getTime()));

                        if(getDuanluqiDevices().contains(device_id)){
                            model.setEpt_stat(createDeviceStateParam(device_id));
                        }

                        device_info.add(model);
                    }


                    deviceProtocol.setDevice_info(device_info);

                    //todo kafka send msg
                    sendMsg(deviceProtocol);
                }
            }
            System.out.println(new Timestamp(item)+"任务完成");
            return item;
        }, pool)
                .whenComplete((s,e)->{

                })
).toArray(CompletableFuture[]:: new);
//所有任务执行完才放行
CompletableFuture.allOf(futureList).join();
//关闭线程池
pool.shutdown();

你可能感兴趣的:(多线程)