当前位置: 首页 > news >正文

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

目录

        • 一、截图示例
        • 二、代码示例
          • 2.1 测试代码
          • 2.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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<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;}
}
http://www.lryc.cn/news/185149.html

相关文章:

  • 在供应链管理中,如何做好库存分析?库存分析有哪些监控指标?
  • 黑豹程序员-架构师学习路线图-百科:Database数据库
  • 你相信光吗?黑灯工厂重新相信“光”
  • Ubuntu 20.04使用源码安装nginx 1.14.0
  • springboot框架拦截器中HttpServletRequest 请求如何区分是图片上传流还是普通的字符流?
  • 简单聊聊 TCP 协议
  • 钡铼BL124PN:简单快速转换Profinet到Ethernet/IP
  • 【golang】go 空结构体 详解 空结构体内容占用及大小
  • 身为产品经理该如何向客户推广API商品数据接口
  • 【数据结构】460. LFU 缓存
  • 文字转语音播报模块(一):阿里云nls服务使用示例
  • Vscode配置C#编程环境(win10)
  • python:xlrd 读取 Excel文件,显示在 tkinterTable 表格中
  • 深度学习——深度学习计算一
  • yolov5及yolov7实战之剪枝
  • 力扣第257题 二叉树的所有路径 c++ 树 深度优先搜索 字符串 回溯 二叉树
  • 保研之旅·终
  • 达梦数据库 视图 错误 [22003]: 数据溢出
  • 【文献阅读】【NMI 2022】LocalTransform :基于广义模板的有机反应性准确预测图神经网络
  • QQ浏览器怎么才能设置默认搜索引擎为百度
  • Go Gin Gorm Casbin权限管理实现 - 3. 实现Gin鉴权中间件
  • js 封装一个异步任务函数
  • 目标检测YOLO实战应用案例100讲-基于无人机航拍图像的目标检测
  • PyQt5配置踩坑
  • 内网渗透笔记之内网基础知识
  • vue3+elementPlus:el-select选择器里添加按钮button
  • Android 模拟点击
  • css自学框架之选项卡
  • Element Plus组件库中的input组件如何点击查看按钮时不可编辑,点击编辑时可编辑使用setup
  • 小米、华为、iPhone、OPPO、vivo如何在手机让几张图拼成一张?