当前位置: 首页 > 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/news/161831.html

相关文章:

  • 【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的使用说明
  • 【react】动态页面转换成html文件下载,解决样式问题
  • Pytorch CIFAR10图像分类 Swin Transformer篇
  • 学会使用这个魔法棒,再也不用在容器里安装乱七八糟的命令工具了!
  • 数据结构如何影响程序的错误检测和调试?
  • Django模板,Django中间件,ORM操作(pymysql + SQL语句),连接池,session和cookie, 缓存
  • N个数求和
  • 时间片轮转调度算法
  • 【CMake入门】第四节——静态库和共享库及安装、使用库的流程
  • [足式机器人]Part2 Dr. CAN学习笔记-数学基础Ch0-6复数Complex Number
  • mysql中information_schema.tables字段说明