From 61860a31907ce5a6cbce0259827abcccc4781f35 Mon Sep 17 00:00:00 2001 From: Wonder Date: Mon, 10 Nov 2025 18:38:05 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=84Update:=20=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E6=8B=92=E7=BB=9D=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/cn/hezhaohui/threadpool/conifg/AsyncConfig.java | 2 ++ .../java/cn/hezhaohui/threadpool/service/AsyncService.java | 1 + .../cn/hezhaohui/threadpool/service/AsyncServiceTest.java | 4 ++-- 3 files changed, 5 insertions(+), 2 deletions(-) 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");