```java //创建一个线程池 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 20, 1L, TimeUnit.SECONDS, new linkedBlockingDeque<>(50), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); CompletableFuturefutureImg = CompletableFuture.supplyAsync(() -> { System.out.println("查询商品的图片信息"); return "hello.jpg"; },threadPoolExecutor); CompletableFuture futureAttr = CompletableFuture.supplyAsync(() -> { try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("查询商品的属性"); return "黑色+256G"; },threadPoolExecutor); CompletableFuture futureDesc = CompletableFuture.supplyAsync(() -> { System.out.println("查询商品介绍"); return "华为"; },threadPoolExecutor); CompletableFuture allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc); allOf.join();//等待所有结果完成 System.out.println("main....end..."+futureImg.get()+"=>"+futureAttr.get()+"=>"+futureDesc.get());
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)