• 【juc】future并行执行并获取返回值


    一、截图示例

    在这里插入图片描述

    二、代码示例
    2.1 接口示例
    package com.learning.controller;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    
    /**
     * @Author wangyouhui
     * @Description TODO
     **/
    @RestController
    @RequestMapping("info")
    @Slf4j
    public class InfoController {
        @GetMapping("/order/{id}")
        public Map orderDetail(@PathVariable Long id){
            log.info("开始查找订单: {}", id);
            try {
                Random random = new Random();
                Thread.sleep(2000 + random.nextInt(2000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Map result = new HashMap<>();
            result.put("id", id);
            result.put("total", 1300);
            log.info("查找订单结束: {}", id);
            return result;
        }
    
        @GetMapping("/product/{id}")
        public Map productDetail(@PathVariable Long id){
            log.info("开始查找产品: {}", id);
            try {
                Random random = new Random();
                Thread.sleep(500 + random.nextInt(2000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Map result = new HashMap<>();
            if(id == 1){
                result.put("id", id);
                result.put("price", 300);
                result.put("name", "小米耳机");
            }else{
                result.put("id", id);
                result.put("price", 1000);
                result.put("name", "三星硬盘");
            }
            log.info("开始查找产品: {}", id);
            return result;
        }
    
        @GetMapping("/package/{id}")
        public Map packageDetail(@PathVariable Long id){
            log.info("开始查找快递: {}", id);
            try {
                Random random = new Random();
                Thread.sleep(3000 + random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Map result = new HashMap<>();
            result.put("id", id);
            result.put("name", "中通快递");
            result.put("id", id);
            log.info("开始查找快递: {}", id);
            return result;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    2.2 调用示例
    package com.learning.future;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.Map;
    import java.util.concurrent.*;
    
    /**
     * @Author wangyouhui
     * @Description future并发执行,获取有返回值的结果
     **/
    @Slf4j
    public class Learning_01 {
        public static void main(String[] args) {
            System.out.println("查询信息开始");
            long begin = System.currentTimeMillis();
            log.info("开始时间: {}", begin);
            ExecutorService executorService = Executors.newCachedThreadPool();
            RestTemplate restTemplate = new RestTemplate();
            Future> future1 = executorService.submit(() -> {
                return restTemplate.getForObject("http://127.0.0.1:8080/info/order/{1}", Map.class, 1);
            });
            Future> future2 = executorService.submit(()->{
                return restTemplate.getForObject("http://127.0.0.1:8080/info/product/{1}", Map.class, 1);
            });
            Future> future3 = executorService.submit(()->{
                return restTemplate.getForObject("http://127.0.0.1:8080/info/product/{1}", Map.class, 2);
            });
            Future> future4 = executorService.submit(()->{
                return restTemplate.getForObject("http://127.0.0.1:8080/info/package/{1}", Map.class, 1);
            });
            try {
                Map map1 = future1.get();
                Map map2 = future2.get();
                Map map3 = future3.get();
                Map map4 = future4.get();
                log.info("订单信息: {}", map1);
                log.info("产品1信息: {}", map2);
                log.info("产品2信息: {}", map3);
                log.info("快递信息: {}", map4);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            executorService.shutdown();
            long end = System.currentTimeMillis();
            log.info("结束时间: {}", end);
            log.info("查询信息结束, 总耗时: {}", end-begin);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
  • 相关阅读:
    val的准确率高于train是过拟合吗
    Word使用小技巧
    想兼职?学网络安全,钱赚到你手软
    第05、WireShark抓包-协议分析
    SpringBoot : ch04 整合数据源
    用74HC573进行LED数码管驱动的编程
    全局事件总线
    U81206:链式前向星模板题
    AI研究领域有哪些?
    基于小程序的理发店预约系统
  • 原文地址:https://blog.csdn.net/qq_32088869/article/details/133689749