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();
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/181875.html