SpringBoot使用异步线程@Async处理请求

springboot中可以使用 @Async 注解开启异步线程处理请求,如要使用异步方法处理请求,需要在主启动类上开启异步处理功能: @EnableAsync 。异步方法默认使用的线程池是 SimpleAsyncTaskExecutor ,该线程池每次有新的请求时都会创建新的线程处理请求:

@Async
public void testAsync() {
    System.out.println(Thread.currentThread().getName());
}

异步线程名输出:
SimpleAsyncTaskExecutor-1
SimpleAsyncTaskExecutor-2
SimpleAsyncTaskExecutor-3
SimpleAsyncTaskExecutor-4
SimpleAsyncTaskExecutor-5
SimpleAsyncTaskExecutor-6
SimpleAsyncTaskExecutor-7
SimpleAsyncTaskExecutor-8
......
SimpleAsyncTaskExecutor-32
SimpleAsyncTaskExecutor-33
SimpleAsyncTaskExecutor-34
SimpleAsyncTaskExecutor-35
SimpleAsyncTaskExecutor-36

所以可以通过配置异步线程池方式指定使用我们的线程池来处理请求,达到线程复用的目的:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xingo
 * @date 2022/12/27
 */
@EnableAsync
@Configuration
public class AsyncThreadPoolConfig {

    /**
     * 线程池核心线程数量
     */
    private int corePoolSize = 8;
    /**
     * 线程池最大线程数量
     */
    private int maxPoolSize = 16;
    /**
     * 线程池中允许线程的空闲时间
     */
    private int keepAliveTime = (int) TimeUnit.SECONDS.toSeconds(30);
    /**
     * 线程池中队列最大数量
     */
    private int queueCapacity = 120;
    /**
     * 线程名称前缀
     */
    private static final String THREAD_PREFIX = "threadpool-async-";

    @Bean
    @Lazy
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();

        threadPool.setCorePoolSize(corePoolSize);
        threadPool.setMaxPoolSize(maxPoolSize);
        threadPool.setKeepAliveSeconds(keepAliveTime);
        threadPool.setQueueCapacity(queueCapacity);
        threadPool.setThreadNamePrefix(THREAD_PREFIX);
        threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPool.initialize();

        return threadPool;
    }
}

通过这样配置就可以在使用异步线程的方法上使用我们配置的线程池处理请求了,只不过使用时需要在注解上指定使用我们的线程池。

@Async("threadPoolTaskExecutor")
public void testAsync() {
    System.out.println(Thread.currentThread().getName());
}

异步线程名输出:
threadpool-async-1
threadpool-async-2
threadpool-async-3
threadpool-async-4
threadpool-async-5
threadpool-async-6
threadpool-async-7
threadpool-async-8
threadpool-async-1
threadpool-async-2
threadpool-async-3
threadpool-async-4
threadpool-async-5
threadpool-async-6
threadpool-async-7
threadpool-async-8
......

通过线程名的输出可以看到线程被复用了。

使用 @Async 注解时需要注意,由于是使用的aop代理方式才会生效,所以调用方发起调用时需要跟被调用方法在不同的类中,或者通过aop代理类调用方法。如果使用代理方式,需要引入依赖:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>

这时在同一个类的不同方法调用时不能通过this或直接方法名调用,需要通过代理方式调用:

@Async("threadPoolTaskExecutor")
public void testAsync() {
    System.out.println(Thread.currentThread().getName());
}

public void testAsync0() {
    TestServer proxy = (TestServer) AopContext.currentProxy();
    proxy.testAsync();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值