From c1726a42981b43f903f4164eac46c0e87acfcb36 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Fri, 31 Dec 2021 08:10:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9B=B4=E5=A4=9A=E7=9A=84?= =?UTF-8?q?=E6=8B=92=E7=BB=9D=E7=AD=96=E7=95=A5=E7=B1=BB=E5=9E=8B.=20(#54)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toolkit/thread/RejectedPolicies.java | 27 ++++++++++--------- .../toolkit/thread/RejectedTypeEnum.java | 4 +-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java index 9cd515ef..d98e143b 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedPolicies.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import java.util.concurrent.BlockingQueue; import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; /** * Rejected policies. @@ -15,12 +16,12 @@ import java.util.concurrent.RejectedExecutionHandler; public class RejectedPolicies { /** - * 发生拒绝事件时, 添加新任务并运行最早的任务 - * - * @return + * 发生拒绝事件时, 添加新任务并运行最早的任务. */ - public static RejectedExecutionHandler runsOldestTaskPolicy() { - return (r, executor) -> { + public static class RunsOldestTaskPolicy implements RejectedExecutionHandler { + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } @@ -33,16 +34,17 @@ public class RejectedPolicies { if (!newTaskAdd) { executor.execute(r); } - }; + } + } /** - * 使用阻塞方法将拒绝任务添加队列, 可保证任务不丢失 - * - * @return + * 使用阻塞方法将拒绝任务添加队列, 可保证任务不丢失. */ - public static RejectedExecutionHandler syncPutQueuePolicy() { - return (r, executor) -> { + public static class SyncPutQueuePolicy implements RejectedExecutionHandler { + + @Override + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } @@ -51,7 +53,8 @@ public class RejectedPolicies { } catch (InterruptedException e) { log.error("Adding Queue task to thread pool failed.", e); } - }; + } + } } diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java index afd186cd..c4e9faa0 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/thread/RejectedTypeEnum.java @@ -42,12 +42,12 @@ public enum RejectedTypeEnum { /** * 发生拒绝事件时, 添加新任务并运行最早的任务 */ - RUNS_OLDEST_TASK_POLICY(5, RejectedPolicies.runsOldestTaskPolicy()), + RUNS_OLDEST_TASK_POLICY(5, new RejectedPolicies.RunsOldestTaskPolicy()), /** * 使用阻塞方法将拒绝任务添加队列, 可保证任务不丢失 */ - SYNC_PUT_QUEUE_POLICY(6, RejectedPolicies.syncPutQueuePolicy()); + SYNC_PUT_QUEUE_POLICY(6, new RejectedPolicies.SyncPutQueuePolicy()); /** * 类型