【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<String, Object> orderDetail(@PathVariable Long id){
        log.info("开始查找订单: {}", id);
        try {
            Random random = new Random();
            Thread.sleep(2000 + random.nextInt(2000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        result.put("total", 1300);
        log.info("查找订单结束: {}", id);
        return result;
    }

    @GetMapping("/product/{id}")
    public Map<String, Object> productDetail(@PathVariable Long id){
        log.info("开始查找产品: {}", id);
        try {
            Random random = new Random();
            Thread.sleep(500 + random.nextInt(2000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map<String, Object> 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<String, Object> packageDetail(@PathVariable Long id){
        log.info("开始查找快递: {}", id);
        try {
            Random random = new Random();
            Thread.sleep(3000 + random.nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        result.put("name", "中通快递");
        result.put("id", id);
        log.info("开始查找快递: {}", id);
        return result;
    }
}

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<Map<String,Object>> future1 = executorService.submit(() -> {
            return restTemplate.getForObject("http://127.0.0.1:8080/info/order/{1}", Map.class, 1);
        });
        Future<Map<String,Object>> future2 = executorService.submit(()->{
            return restTemplate.getForObject("http://127.0.0.1:8080/info/product/{1}", Map.class, 1);
        });
        Future<Map<String,Object>> future3 = executorService.submit(()->{
            return restTemplate.getForObject("http://127.0.0.1:8080/info/product/{1}", Map.class, 2);
        });
        Future<Map<String,Object>> future4 = executorService.submit(()->{
            return restTemplate.getForObject("http://127.0.0.1:8080/info/package/{1}", Map.class, 1);
        });
        try {
            Map<String, Object> map1 = future1.get();
            Map<String, Object> map2 = future2.get();
            Map<String, Object> map3 = future3.get();
            Map<String, Object> 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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值