diff --git a/src/main/java/cn/hezhaohui/threadpool/conifg/AsyncConfig.java b/src/main/java/cn/hezhaohui/threadpool/conifg/AsyncConfig.java index 98bc629..afaf452 100644 --- a/src/main/java/cn/hezhaohui/threadpool/conifg/AsyncConfig.java +++ b/src/main/java/cn/hezhaohui/threadpool/conifg/AsyncConfig.java @@ -6,6 +6,7 @@ import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; /** * 异步配置类 @@ -22,6 +23,7 @@ public class AsyncConfig { executor.setQueueCapacity(100); executor.setThreadNamePrefix("Custom-Async-"); executor.initialize(); + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy()); return executor; } } diff --git a/src/main/java/cn/hezhaohui/threadpool/service/AsyncService.java b/src/main/java/cn/hezhaohui/threadpool/service/AsyncService.java index c8c8ac5..1c4c95e 100644 --- a/src/main/java/cn/hezhaohui/threadpool/service/AsyncService.java +++ b/src/main/java/cn/hezhaohui/threadpool/service/AsyncService.java @@ -39,6 +39,7 @@ public class AsyncService { Thread.currentThread().interrupt(); } finally { latch.countDown(); + log.info("⭐ {}", latch.getCount()); } } } diff --git a/src/test/java/cn/hezhaohui/threadpool/service/AsyncServiceTest.java b/src/test/java/cn/hezhaohui/threadpool/service/AsyncServiceTest.java index 307fce0..6616221 100644 --- a/src/test/java/cn/hezhaohui/threadpool/service/AsyncServiceTest.java +++ b/src/test/java/cn/hezhaohui/threadpool/service/AsyncServiceTest.java @@ -18,8 +18,8 @@ class AsyncServiceTest { @Test void asyncPrint() throws InterruptedException { - CountDownLatch latch = new CountDownLatch(15); - for (int i = 0; i < 200; i++) { + CountDownLatch latch = new CountDownLatch(100); + for (int i = 0; i < 100; i++) { asyncService.asyncPrint(latch); } log.info("\n[+++ All Tasks Commited +++]\n");