使用线程池的异步编排合并多个接口的查询数据返回给前端页面

使用线程池的异步编排合并多个接口的查询数据返回给前端页面,第1张

使用线程池的异步编排合并多个接口的查询数据返回给前端页面
```java
 //创建一个线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 20, 1L, TimeUnit.SECONDS, new linkedBlockingDeque<>(50), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        CompletableFuture futureImg = 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());
					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5684058.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存