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

查询接口优化(线程池异步调用)

文章目录

  • 前言
  • 一、配置线程池
    • application.yml
    • 参数配置类
    • 线程池配置类
  • 二、服务类


前言

在项目中遇到一个接口响应时间比较慢,查看代码发现是有几个sql查询加在一起比较慢,而且同时业务只需要这几个sql的结果进行拼接处理。
因此考虑使用线程池异步运行多个sql的方式来提高接口响应时间。

以下为相关代码实现

一、配置线程池

application.yml

thread-pool:config:core-size: 8max-size: 16queue-capacity: 64keep-alive-seconds: 180

参数配置类

@Component
@ConfigurationProperties(prefix = "thread-pool.config")
@Data
public class TestThreadPoolConfig {private Integer coreSize;private Integer maxSize;private Integer queueCapacity;private Integer keepAliveSeconds;
}

线程池配置类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.RejectedExecutionHandler;
@Configuration
@EnableAsync
public class ThreadPoolConfig {@Autowiredprivate TestThreadPoolConfig testThreadPoolConfig;/*** @return*/@Bean(name = "testExecutor")public ThreadPoolTaskExecutor testThreadPoolExecutor() {return getAsyncTaskExecutor("test-Executor-", testThreadPoolConfig.getCoreSize(),testThreadPoolConfig.getMaxSize(), testThreadPoolConfig.getQueueCapacity(),testThreadPoolConfig.getKeepAliveSeconds(), null);}/*** 统一异步线程池** @param threadNamePrefix* @param corePoolSize* @param maxPoolSize* @param queueCapacity* @param keepAliveSeconds* @param rejectedExecutionHandler 拒接策略 没有填null* @return*/private ThreadPoolTaskExecutor getAsyncTaskExecutor(String threadNamePrefix, int corePoolSize, int maxPoolSize, int queueCapacity, int keepAliveSeconds, RejectedExecutionHandler rejectedExecutionHandler) {ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();taskExecutor.setCorePoolSize(corePoolSize);taskExecutor.setMaxPoolSize(maxPoolSize);taskExecutor.setQueueCapacity(queueCapacity);taskExecutor.setThreadPriority(Thread.MAX_PRIORITY);//线程优先级taskExecutor.setDaemon(false);//是否为守护线程taskExecutor.setKeepAliveSeconds(keepAliveSeconds);taskExecutor.setThreadNamePrefix(threadNamePrefix);taskExecutor.setRejectedExecutionHandler(rejectedExecutionHandler);taskExecutor.initialize();//线程池初始化return taskExecutor;}
}

二、服务类

    @Autowiredprivate ThreadPoolTaskExecutor testExecutor;@Overridepublic List test() {CompletableFuture<List> future = CompletableFuture.supplyAsync(this::test01, testExecutor).exceptionally(this::handleException);CompletableFuture<List> future2 = CompletableFuture.supplyAsync(this::test02, testExecutor).exceptionally(this::handleException);CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future, future2);List vos = new ArrayList<>();combinedFuture.thenAccept(v -> {vos.addAll(future.join());vos.addAll(future2.join());System.out.println(vos);});combinedFuture.join();return vos;}private List handleException(Throwable t) {throw new RuntimeException("An exception occurred: " + t.getMessage(), t);}public List test01(){System.out.println("执行test01");try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}List list = new ArrayList();list.add("111");list.add("222");System.out.println("执行test01结束");return list;}public List test02(){System.out.println("执行test02");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}List list = new ArrayList();list.add("333");list.add("444");System.out.println("执行test02结束");return list;}


http://www.mrgr.cn/news/97.html

相关文章:

  • 容器构建superset
  • 云计算29-------mysql主从数据库(5.7版本)与python的交互及mycat
  • 电脑音频转文字软件哪个好?不容错过的免费音频转文字软件大盘点
  • 17.1 HTTP协议与Web架构
  • 微信小程序的四种弹窗使用
  • Arthas 学习笔记
  • CSS 的文字描边属性text-stroke
  • XSS Game靶场练习
  • 【机器学习第11章——特征选择与稀疏学习】
  • 决策支持系统:智能化决策的核心要点|DSS|数据|模型|界面|模拟|预测|知识库
  • 【漫谈C语言和嵌入式004】深入理解RS232、RS422和RS485:嵌入式系统中的串行通信协议
  • 递归--数据结构--黑马
  • 微服务架构设计中的常见的10种设计模式
  • 趋动科技成为GSMA 5G IN创新会员,专注于软件定义AI算力技术
  • Leetcode每日刷题之18.四数之和
  • 无人机电池充电器技术详解
  • 阅文集团:摇不动的IP摇钱树
  • 计算机毕业设计 医院问诊系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
  • nginx-1.24交叉编译mips-linux-gnu-gcc
  • 基于SpringBoot的网络海鲜市场系统的设计与实现