我使用的是spring boot 2.0.2,并使用Async来执行任务。
我注册一个TaskExecutor来控制线程池大小,如下所示:
public class ThreadConfig {
@Bean
public TaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(3);
executor.setQueueCapacity(3);
executor.setThreadNamePrefix("anniversary");
executor.initialize();
System.out.println("******* name " + executor.getThreadNamePrefix());
System.out.println("********** core pool size " + executor.getCorePoolSize());
return executor;
}
}
但是在运行期间,创建了超过20个线程,
*********Thread[SimpleAsyncTaskExecutor-1,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-2,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-3,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-4,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-5,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-6,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-7,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-8,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-9,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-10,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-11,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-12,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-13,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********
**********Thread[SimpleAsyncTaskExecutor-14,5,com.abc.team1.project1.anniversary.AnniversaryApplication]*********