🔄Update: 加入拒绝策略

This commit is contained in:
2025-11-10 18:38:05 +08:00
parent fc9972df3b
commit 61860a3190
3 changed files with 5 additions and 2 deletions
@@ -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;
}
}
@@ -39,6 +39,7 @@ public class AsyncService {
Thread.currentThread().interrupt();
} finally {
latch.countDown();
log.info("⭐ {}", latch.getCount());
}
}
}
@@ -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");