【juc】countdownlatch实现并发网络请求

目录

        • 一、截图示例
        • 二、代码示例
          • 2.1 测试代码
          • 2.2 接口代码

一、截图示例

【juc】countdownlatch实现并发网络请求_第1张图片
【juc】countdownlatch实现并发网络请求_第2张图片

二、代码示例
2.1 测试代码
package com.learning.countdownlatch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author wangyouhui
 * @Description 异步查询信息
 **/
@Slf4j
public class Learning_03 {
    public static void main(String[] args) {
        test1();
        test2();
    }

    private static void test1() {
        System.out.println("查询信息开始");
        long begin = System.currentTimeMillis();
        log.info("开始时间: {}", begin);
        RestTemplate restTemplate = new RestTemplate();
        long start = System.currentTimeMillis();
        Map map = restTemplate.getForObject("http://127.0.0.1:8080/info/order/{1}", Map.class, 1);
        log.info("订单信息: {}, 耗时: {}", map, System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
        map = restTemplate.getForObject("http://127.0.0.1:8080/info/product/{1}", Map.class, 1);
        log.info("产品1信息: {}, 耗时: {}", map, System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
        map = restTemplate.getForObject("http://127.0.0.1:8080/info/product/{1}", Map.class, 2);
        log.info("产品2信息: {}, 耗时: {}", map, System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
        map = restTemplate.getForObject("http://127.0.0.1:8080/info/package/{1}", Map.class, 1);
        log.info("快递信息: {}, 耗时: {}", map, System.currentTimeMillis() - start);
        long end = System.currentTimeMillis();
        log.info("结束时间: {}", end);
        log.info("查询信息结束, 总耗时: {}", end-begin);
    }

    private static void test2() {
        System.out.println("查询信息开始");
        long begin = System.currentTimeMillis();
        log.info("开始时间: {}", begin);
        CountDownLatch countDownLatch = new CountDownLatch(4);
        ExecutorService executorService = Executors.newCachedThreadPool();
        RestTemplate restTemplate = new RestTemplate();
        executorService.submit(()->{
            long start = System.currentTimeMillis();
            Map map = restTemplate.getForObject("http://127.0.0.1:8080/info/order/{1}", Map.class, 1);
            log.info("订单信息: {}, 耗时: {}", map, System.currentTimeMillis() - start);
            countDownLatch.countDown();
        });
        executorService.submit(()->{
            long start = System.currentTimeMillis();
            Map map = restTemplate.getForObject("http://127.0.0.1:8080/info/product/{1}", Map.class, 1);
            log.info("产品1信息: {}, 耗时: {}", map, System.currentTimeMillis() - start);
            countDownLatch.countDown();
        });
        executorService.submit(()->{
            long start = System.currentTimeMillis();
            Map map = restTemplate.getForObject("http://127.0.0.1:8080/info/product/{1}", Map.class, 2);
            log.info("产品2信息: {}, 耗时: {}", map, System.currentTimeMillis() - start);
            countDownLatch.countDown();
        });
        executorService.submit(()->{
            long start = System.currentTimeMillis();
            Map map = restTemplate.getForObject("http://127.0.0.1:8080/info/package/{1}", Map.class, 1);
            log.info("快递信息: {}, 耗时: {}", map, System.currentTimeMillis() - start);
            countDownLatch.countDown();
        });
        // 主线程等待
        try{
            countDownLatch.await();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        executorService.shutdown();
        long end = System.currentTimeMillis();
        log.info("结束时间: {}", end);
        log.info("查询信息结束, 总耗时: {}", end-begin);
    }
}

2.2 接口代码
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;
    }
}

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