From 0655b26b73b76d83e539a038d39da175975f842b Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 20 Jun 2025 18:00:15 +0800 Subject: [PATCH] =?UTF-8?q?RetryableTask:=20=E9=81=BF=E5=85=8D=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E6=AC=A1=E4=BB=BB=E5=8A=A1=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E7=BA=BF=E7=A8=8B=E7=9D=A1=E7=9C=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/v7/core/thread/RetryUtil.java | 14 +++++++------- .../cn/hutool/v7/core/thread/RetryableTask.java | 14 +++++++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/thread/RetryUtil.java b/hutool-core/src/main/java/cn/hutool/v7/core/thread/RetryUtil.java index 3927dd3f5..b3b6e5a23 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/thread/RetryUtil.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/thread/RetryUtil.java @@ -37,7 +37,7 @@ public class RetryUtil { * 没有返回值,重试执行方法 * * @param run 执行方法 - * @param maxAttempts 最大的重试次数 + * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次 * @param delay 重试间隔 * @param recover 达到最大重试次数后执行的备用方法 * @param exs 指定的异常类型需要重试 @@ -63,7 +63,7 @@ public class RetryUtil { * 有返回值,重试执行方法 * * @param sup 执行方法 - * @param maxAttempts 最大的重试次数 + * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次 * @param delay 重试间隔 * @param recover 达到最大重试次数后执行的备用方法 * @param exs 指定的异常类型需要重试 @@ -88,10 +88,10 @@ public class RetryUtil { * 没有返回值,重试执行方法 * * @param run 执行方法 - * @param maxAttempts 最大的重试次数 + * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次 * @param delay 重试间隔 * @param recover 达到最大重试次数后执行的备用方法 - * @param predicate 自定义重试条件 + * @param predicate 自定义重试条件, 返回true时表示重试 */ public static void ofPredicate(final Runnable run, final long maxAttempts, final Duration delay, final Supplier recover, final BiPredicate predicate) { @@ -105,14 +105,14 @@ public class RetryUtil { /** - * 根据异常信息进行重试 + * 根据自定义结果进行重试 * 有返回值,重试执行方法 * * @param sup 执行方法 - * @param maxAttempts 最大的重试次数 + * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次 * @param delay 重试间隔 * @param recover 达到最大重试次数后执行的备用方法 - * @param predicate 自定义重试条件 + * @param predicate 自定义重试条件, 返回true时表示重试 * @param 结果类型 * @return 执行结果 */ diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/thread/RetryableTask.java b/hutool-core/src/main/java/cn/hutool/v7/core/thread/RetryableTask.java index ff41f7eae..9c639acae 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/thread/RetryableTask.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/thread/RetryableTask.java @@ -109,11 +109,11 @@ public class RetryableTask { */ private T result; /** - * 执行法方法 + * 执行方法 */ private final Supplier sup; /** - * 重试策略 + * 重试策略, 返回true时表示重试 */ private final BiPredicate predicate; /** @@ -223,7 +223,8 @@ public class RetryableTask { private RetryableTask doExecute() { Throwable th = null; - while (--this.maxAttempts >= 0) { + // 任务至少被执行一次 + do { try { this.result = this.sup.get(); } catch (final Throwable t) { @@ -236,8 +237,11 @@ public class RetryableTask { break; } - ThreadUtil.sleep(delay.toMillis()); - } + // 避免最后一次任务执行时的线程睡眠 + if (this.maxAttempts > 0) { + ThreadUtil.sleep(delay.toMillis()); + } + } while (--this.maxAttempts >= 0); this.throwable = th; return this;