现在有多个异步任务需要处理,考虑用阻塞队列
给线程池提交任务: 将future结果加入阻塞队列
这样先执行完成的可以先消费
BlockingQueue<Integer> bq = new LinkedBlockingQueue<>(); //电商S1报价异步进入阻塞队列 executor.execute(()-> bq.put(f1.get())); //电商S2报价异步进入阻塞队列 executor.execute(()-> bq.put(f2.get())); //电商S3报价异步进入阻塞队列 executor.execute(()-> bq.put(f3.get()));
//异步保存所有报价 for (int i=0; i<3; i++) { Integer r = bq.take(); executor.execute(()->save(r)); }