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

目录

        • 一、截图示例
        • 二、代码示例
          • 2.1 接口示例
          • 2.2 调用示例

一、截图示例

【juc】future并行执行并获取返回值_第1张图片

二、代码示例
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;
    }
}

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);
    }
}

你可能感兴趣的:(juc,juc)