微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐