springboot @async线程池使用以及oom问题
声明线程池
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadPoolExecutor; @Configuration @EnableAsync public class ExecutorConfig1 { @Bean("taskExecutor") public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setCorePoolSize(10); threadPoolTaskExecutor.setMaxPoolSize(10); threadPoolTaskExecutor.setQueueCapacity(20); threadPoolTaskExecutor.setKeepAliveSeconds(3000); threadPoolTaskExecutor.setThreadNamePrefix("Async-Service-"); threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); return threadPoolTaskExecutor; } }
业务逻辑类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; @Component public class QueryDataExec { @Autowired private TaskDataExec taskDataExec; public void execData() { ArrayList
逻辑执行类,注意@Asycn 的处理类不能和调用一个类
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @Component public class TaskDataExec { Logger log = LoggerFactory.getLogger(TaskDataExec.class); @Async("taskExecutor") public void taskDataExec(String id) { log.info("开始处理:" + id); log.info("完成处理:" + id); } }
测试类
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class UdpApplicationTests { @Test void contextLoads() { System.out.println("123"); } @Autowired private QueryDataExec queryDataExec; @Test public void dataExecTest() { queryDataExec.execData(); } }
效果,注意Async-Service后面的数字是对应的线程池
2021-04-13 16:17:19.290 INFO 7280 --- [Async-Service-1] com.udp.TaskDataExec : 开始处理:id-0 2021-04-13 16:17:19.291 INFO 7280 --- [Async-Service-1] com.udp.TaskDataExec : 完成处理:id-0 2021-04-13 16:17:19.292 INFO 7280 --- [Async-Service-3] com.udp.TaskDataExec : 开始处理:id-2 2021-04-13 16:17:19.292 INFO 7280 --- [Async-Service-3] com.udp.TaskDataExec : 完成处理:id-2 2021-04-13 16:17:19.303 INFO 7280 --- [Async-Service-2] com.udp.TaskDataExec : 开始处理:id-1 2021-04-13 16:17:19.303 INFO 7280 --- [Async-Service-2] com.udp.TaskDataExec : 完成处理:id-1 2021-04-13 16:17:19.304 INFO 7280 --- [Async-Service-4] com.udp.TaskDataExec : 开始处理:id-3 2021-04-13 16:17:19.306 INFO 7280 --- [Async-Service-4] com.udp.TaskDataExec : 完成处理:id-3 2021-04-13 16:17:19.309 INFO 7280 --- [Async-Service-5] com.udp.TaskDataExec : 开始处理:id-4 2021-04-13 16:17:19.309 INFO 7280 --- [Async-Service-5] com.udp.TaskDataExec : 完成处理:id-4 2021-04-13 16:17:19.312 INFO 7280 --- [Async-Service-6] com.udp.TaskDataExec : 开始处理:id-5 2021-04-13 16:17:19.312 INFO 7280 --- [Async-Service-6] com.udp.TaskDataExec : 完成处理:id-5 2021-04-13 16:17:19.315 INFO 7280 --- [Async-Service-7] com.udp.TaskDataExec : 开始处理:id-6 2021-04-13 16:17:19.316 INFO 7280 --- [Async-Service-7] com.udp.TaskDataExec : 完成处理:id-6 2021-04-13 16:17:19.316 INFO 7280 --- [sync-Service-10] com.udp.TaskDataExec : 开始处理:id-9 2021-04-13 16:17:19.316 INFO 7280 --- [sync-Service-10] com.udp.TaskDataExec : 完成处理:id-9 2021-04-13 16:17:19.316 INFO 7280 --- [Async-Service-8] com.udp.TaskDataExec : 开始处理:id-7 2021-04-13 16:17:19.317 INFO 7280 --- [Async-Service-8] com.udp.TaskDataExec : 完成处理:id-7 2021-04-13 16:17:19.318 INFO 7280 --- [Async-Service-9] com.udp.TaskDataExec : 开始处理:id-8 2021-04-13 16:17:19.318 INFO 7280 --- [Async-Service-9] com.udp.TaskDataExec : 完成处理:id-8