Java多线程优化接口响应

ops/2024/9/24 2:47:44/

同步查询

java"> @Override
public MallOrder getById1(Long id) {long startTime = System.currentTimeMillis();MallOrder mallOrder = new MallOrder();mallOrder.setId(1L);mallOrder.setShopId(3L);mallOrder.setCustomerId(78L);mallOrder.setGoodsId(664L);mallOrder.setOrderTime(LocalDateTime.now());mallOrder.setOrderStatus(1);mallOrder.setTotalAmount(new BigDecimal("129.8"));// 商品Long goodsId = mallGoodsService.getById(mallOrder.getGoodsId());log.info("商品:" + goodsId);// 顾客Long customerId = mallCustomerService.getById(mallOrder.getCustomerId());log.info("顾客:" + customerId);// 店铺Long shopId = mallShopService.getById(mallOrder.getShopId());log.info("店铺:" + shopId);try {Thread.sleep(150);} catch (InterruptedException e) {throw new RuntimeException(e);}long endTime = System.currentTimeMillis();log.info("耗时:" + (endTime - startTime));return mallOrder;
}
java">package com.qiangesoft.multithread.service.impl;import com.qiangesoft.multithread.service.IMallShopService;
import org.springframework.stereotype.Service;/*** <p>* 店铺信息 服务实现类* </p>** @author qiangesoft* @since 2024-05-06*/
@Service
public class MallShopServiceImpl implements IMallShopService {@Overridepublic Long getById(Long id) {try {Thread.sleep(350);} catch (InterruptedException e) {throw new RuntimeException(e);}return id;}
}
java">package com.qiangesoft.multithread.service.impl;import com.qiangesoft.multithread.service.IMallGoodsService;
import org.springframework.stereotype.Service;/*** <p>* 商品信息 服务实现类* </p>** @author qiangesoft* @since 2024-05-06*/
@Service
public class MallGoodsServiceImpl implements IMallGoodsService {@Overridepublic Long getById(Long id) {try {Thread.sleep(280);} catch (InterruptedException e) {throw new RuntimeException(e);}return id;}
}
java">package com.qiangesoft.multithread.service.impl;import com.qiangesoft.multithread.service.IMallCustomerService;
import org.springframework.stereotype.Service;/*** <p>* 客户信息 服务实现类* </p>** @author qiangesoft* @since 2024-05-06*/
@Service
public class MallCustomerServiceImpl implements IMallCustomerService {@Overridepublic Long getById(Long id) {try {Thread.sleep(230);} catch (InterruptedException e) {throw new RuntimeException(e);}return id;}
}

在这里插入图片描述

异步优化

  • 核心线程数不变,仍旧是 cpu 数;
  • 最大线程数不变,仍旧是 cpu 数的5倍;
  • 空闲线程存活时间不变,仍旧是 5 分钟;
  • 使用 SynchronousQueue 替代 LinkedBlockingQueue(1024)。SynchronousQueue 是一个特殊的队列,其最大容量是1。也就是说,任何一次插入操作都必须等待一个相应的删除操作,反之亦然。如果没有相应的操作正在进行,则该线程将被阻塞;
  • 指定拒绝策略为 CallerRunsPolicy。当线程池资源不够时,由主线程来执行任务;

ps:在这个配置下,及时线程池中的所有资源全部耗尽,也只会降级到串行执行,不会让系统变的更糟糕。

java">private static int coreSize = Runtime.getRuntime().availableProcessors();private final ExecutorService executorService = new ThreadPoolExecutor(coreSize,coreSize * 5,5L,TimeUnit.MINUTES,// 最大容量为1的队列new SynchronousQueue<>(),// 拒绝策略,降级到主线程执行new ThreadPoolExecutor.CallerRunsPolicy());@Overridepublic MallOrder getById(Long id) {long startTime = System.currentTimeMillis();MallOrder mallOrder = new MallOrder();mallOrder.setId(1L);mallOrder.setShopId(3L);mallOrder.setCustomerId(78L);mallOrder.setGoodsId(664L);mallOrder.setOrderTime(LocalDateTime.now());mallOrder.setOrderStatus(1);mallOrder.setTotalAmount(new BigDecimal("129.8"));// 商品Future<Long> goodsFuture = executorService.submit(() -> mallGoodsService.getById(mallOrder.getGoodsId()));// 顾客Future<Long> customerFuture = executorService.submit(() -> mallCustomerService.getById(mallOrder.getCustomerId()));// 店铺Future<Long> shopFuture = executorService.submit(() -> mallShopService.getById(mallOrder.getShopId()));try {Thread.sleep(150);} catch (InterruptedException e) {throw new RuntimeException(e);}try {log.info("商品:" + goodsFuture.get());} catch (Exception e) {throw new RuntimeException(e);}try {log.info("顾客:" + customerFuture.get());} catch (Exception e) {throw new RuntimeException(e);}try {log.info("店铺:" + shopFuture.get());} catch (Exception e) {throw new RuntimeException(e);}long endTime = System.currentTimeMillis();log.info("耗时:" + (endTime - startTime));return mallOrder;}

在这里插入图片描述


http://www.ppmy.cn/ops/35637.html

相关文章

Kubernetes的labels解析

目录 1、基本概念2、核心特性3、使用场景4、最佳实践5、使用示例 在 Kubernetes 中&#xff0c;labels 是一种强大的元数据标注机制&#xff0c;用于标记 Kubernetes 对象&#xff0c;如 Pods、Services、Deployments 等。它们是键值对&#xff08;key-value pairs&#xff09;…

《鸿蒙发展历程记录》

鸿蒙&#xff0c;作为华为公司倾力打造的操作系统&#xff0c;它的发展历程堪称一部科技传奇。 鸿蒙的起源可以追溯到多年以前&#xff0c;当时华为就已经意识到自主操作系统的重要性&#xff0c;开始了前瞻性的规划和布局。 在发展过程中&#xff0c;鸿蒙经历了不断的迭代和完…

腾讯会议崩溃解决

突然腾讯会议就罢工了&#xff0c;腾讯会议的主界面可以登陆上去&#xff0c;不会异常退出&#xff1a; 这时无论是通过别人提供的会议号“加入会议” 还是 “快速会议”&#xff0c;都会出现下面的异常&#xff0c;并崩溃退出&#xff1a; 在网上搜“SteinwayMSVCRT”导致的腾…

C语言三维数组的创建

#include <stdio.h> int main() { int n 5; // 假设我们想要一个 5x5x5 的三维数组 // 但这样声明是不允许的&#xff0c;因为 n 的值在编译时未知 // int f[n][n][n]; // 错误 // 如果 n 是一个编译时常量&#xff08;例如枚举值或 const 变量&#xff09;&#x…

Linux安装Python3.9环境

大家好&#xff0c;今天给大家分享一下在Linux系统中安装Python3环境&#xff0c;Linux系统中自带的Python2尽量不要删除&#xff0c;删除后可能会导致系统出现问题。 关于Linux常用命令&#xff0c;可以参考&#xff1a;作为测试人员的Linux常用命令 一、下载Python3安装包 …

在Excel中使用正则提取单元格内容

在办公自动化的浪潮中&#xff0c;Excel 作为数据处理的利器&#xff0c;一直在不断进化。最近&#xff0c;我注意到了不坑盒子Office插件一个非常实用的功能更新——bk_regex_string 公式。这个功能对于我们这些日常需要处理大量文本和数据的办公人员来说&#xff0c;无疑是一…

Activating More Pixels in Image Super-Resolution Transformer

cvpr2023https://github.com/XPixelGroup/HAT?tabreadme-ov-file问题引入&#xff1a; – 现在的transformer based的SR模型“感受野”不够&#xff1b; – 分析&#xff1a;原本认为transformer-based的方法优于CNN-based的方法是因为可以利用更加long-range的信息&#xff0…

Spring框架第六章(SpringMVC概括及基于JDK21与Tomcat10创建SpringMVC程序)

文章目录 一、什么是MVC二、什么是SpringMVC三、基于JDK21与Tomcat 10创建SpringMVC程序 一、什么是MVC MVC是一种软件架构模式&#xff08;是一种软件架构设计思想&#xff0c;不止Java开发中用到&#xff0c;其它语言也需要用到&#xff09;&#xff0c;它将应用分为三块&am…