Springboot中启用@Async

 直接上代码吧,然后再需要使用的异步的方法上添加@Async

package com.pobo.liangtest.redis.config;

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


/**
 * @author liang
 * @version 1.0-SNAPSHOT
 * @description
 * @jdk-v 1.8.0_161
 * @date 2020/9/1 9:40
 * @update
 */
@Configuration
@EnableAsync
public class AsyncConfig {

    private static final int MAX_POOL_SIZE = 20;//线程池最大线程数量
    private static final int CORE_POOL_SIZE = 10;//核心线程数量
    private static final int QUEUE_CAPACITY = 10000;//队列容量

    @Bean("asyncTaskExecutor")
    public AsyncTaskExecutor asyncTaskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
        taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
        taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
        taskExecutor.setThreadNamePrefix("async-task-thread-pool-");
        taskExecutor.initialize();
        return taskExecutor;
    }
}

 


版权声明:本文为qq_40006446原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。