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

异步线程实现简单实现方式@Async

初始化配置EnableAsync


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: wangxiaofeng
 * @Description
 * @date 2023/12/6 16:45
 * @Version: 1.0
 */
@Configuration
@EnableAsync
public class TheadPoolConfig implements AsyncConfigurer {
    @Value("${ThreadPoolConfig.corePoolSize:50}")
    private int coreSize;

    @Value("${ThreadPoolConfig.maxPoolSize:200}")
    private int maxSize;

    @Value("${ThreadPoolConfig.queueCapacity:100}")
    private int queueCapacity;

    @Value("${ThreadPoolConfig.keepAlive:100}")
    private int keepAliveSeconds;

    @Value("${ThreadPoolConfig.otherCoreSize:50}")
    private int otherCoreSize;
    @Value("${ThreadPoolConfig.otherMaxPoolSize:200}")
    private int otherMaxSize;

    @Value("${ThreadPoolConfig.otherQueueCapacity:100}")
    private int otherQueueCapacity;

    @Value("${ThreadPoolConfig.otherKeepAlive:100}")
    private int otherKeepAliveSeconds;



    @Bean("asyncThreadPool")
    public AsyncTaskExecutor taskExecutor1() {
        //使用Spring内置线程池任务对象
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //设置线程池参数
        taskExecutor.setThreadNamePrefix("asyncThreadPool-");
        taskExecutor.setCorePoolSize(coreSize);
        taskExecutor.setMaxPoolSize(maxSize);
        taskExecutor.setQueueCapacity(queueCapacity);
        taskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.initialize();
        return taskExecutor;
    }


}

使用

    @Async("asyncThreadPool")
    public void updateHotelPriceMap(String hotelId) {

}


http://www.kler.cn/a/161831.html

相关文章:

  • makefile 设置动态库路径参数
  • 从华为到创业公司
  • Mysql数据库里的SSH连接
  • React的基础API介绍(二)
  • 06.VSCODE:备战大项目,CMake专项配置
  • 搭建Python2和Python3虚拟环境
  • 【AIGC】prompt工程从入门到精通--图片生成专题
  • JS的变量提升ES6基础
  • 大数据项目——基于Django/协同过滤算法的房源可视化分析推荐系统的设计与实现
  • UE Websocket笔记
  • JAVA IO:NIO
  • IntelliJ IDEA 2023.3 最新变化
  • 力扣每日一题day30[226. 翻转二叉树]
  • Web server failed to start. Port 8888 was already in use.
  • 点评项目——商户查询缓存
  • 前端实现token无感刷新的原因和步骤
  • Linux Docker 安装Nginx
  • 【Linux】Java 程序员必会的 Linux 最常用的命令
  • 小纸条..
  • ubuntu源配置文件/etc/apt/sources.list不存在
  • C语言实现水仙花
  • PostgreSQL 技术内幕(十二) CloudberryDB 并行化查询之路
  • Python文件操作(txt + xls + json)
  • class035 数据结构设计高频题【算法】
  • Chromium包含的内容(引擎)
  • Eureka的使用说明