1 介绍
Spring是通过任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor来创建一个基于线城池的TaskExecutor。在使用线程池的大多数情况下都是异步非阻塞的。我们配置注解@EnableAsync可以开启异步任务。然后在实际执行的方法上配置注解@Async上声明是异步任务。
2 配置类
package com.example.asyn_thread;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class ThreadConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);//线程池维护线程的最少数量
executor.setMaxPoolSize(15);//线程池维护线程的最大数量
executor.setQueueCapacity(25);//线程池所使用的缓冲队列
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
解读:
利用EnableAsync来开启Springboot对于异步任务的支持
配置类实现接口AsyncConfigurator,返回一个ThreadPoolTaskExecutor线程池对象。
对于线程池的配置:
- 如果池中的实际线程数小于corePoolSize,无论是否其中有空闲的线程,都会给新的任务产生新的线程
- 如果池中的线程数>corePoolSize and <maximumPoolSize,而又有空闲线程,就给新任务使用空闲线程,如没有空闲线程,则产生新线程
- 如果池中的线程数=maximumPoolSize,则有空闲线程使用空闲线程,否则新任务放入workQueue。(线程的空闲只有在workQueue中不再有任务时才成立)
3 任务执行
package com.example.asyn_thread;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncTaskService {
@Async
public void executeAsyncTask(int i) {
System.out.println("线程" + Thread.currentThread().getName() + " 执行异步任务:" + i);
}
}
通过@Async注解表明该方法是异步方法,如果注解在类上,那表明这个类里面的所有方法都是异步的。
4 测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class AsynThreadApplicationTests {
@Autowired
private AsyncTaskService asyncTaskService;
@Test
public void contextLoads() {
}
@Test
public void threadTest() {
for (int i = 0; i < 20; i++) {
asyncTaskService.executeAsyncTask(i);
}
}
}
执行结果:
线程ThreadPoolTaskExecutor-1 执行异步任务:0
线程ThreadPoolTaskExecutor-1 执行异步任务:5
线程ThreadPoolTaskExecutor-1 执行异步任务:6
线程ThreadPoolTaskExecutor-1 执行异步任务:7
线程ThreadPoolTaskExecutor-1 执行异步任务:8
线程ThreadPoolTaskExecutor-1 执行异步任务:9
线程ThreadPoolTaskExecutor-1 执行异步任务:10
线程ThreadPoolTaskExecutor-1 执行异步任务:11
线程ThreadPoolTaskExecutor-1 执行异步任务:12
线程ThreadPoolTaskExecutor-1 执行异步任务:13
线程ThreadPoolTaskExecutor-1 执行异步任务:14
线程ThreadPoolTaskExecutor-1 执行异步任务:15
线程ThreadPoolTaskExecutor-1 执行异步任务:16
线程ThreadPoolTaskExecutor-1 执行异步任务:17
线程ThreadPoolTaskExecutor-1 执行异步任务:18
线程ThreadPoolTaskExecutor-1 执行异步任务:19
线程ThreadPoolTaskExecutor-3 执行异步任务:2
线程ThreadPoolTaskExecutor-2 执行异步任务:1
线程ThreadPoolTaskExecutor-4 执行异步任务:3
线程ThreadPoolTaskExecutor-5 执行异步任务:4