RetryableTask: 避免最后一次任务执行时的线程睡眠

This commit is contained in:
Looly 2025-06-20 18:00:15 +08:00
parent 3e56130847
commit 0655b26b73
2 changed files with 16 additions and 12 deletions

View File

@ -37,7 +37,7 @@ public class RetryUtil {
* 没有返回值重试执行方法 * 没有返回值重试执行方法
* *
* @param run 执行方法 * @param run 执行方法
* @param maxAttempts 最大的重试次数 * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次
* @param delay 重试间隔 * @param delay 重试间隔
* @param recover 达到最大重试次数后执行的备用方法 * @param recover 达到最大重试次数后执行的备用方法
* @param exs 指定的异常类型需要重试 * @param exs 指定的异常类型需要重试
@ -63,7 +63,7 @@ public class RetryUtil {
* 有返回值重试执行方法 * 有返回值重试执行方法
* *
* @param sup 执行方法 * @param sup 执行方法
* @param maxAttempts 最大的重试次数 * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次
* @param delay 重试间隔 * @param delay 重试间隔
* @param recover 达到最大重试次数后执行的备用方法 * @param recover 达到最大重试次数后执行的备用方法
* @param exs 指定的异常类型需要重试 * @param exs 指定的异常类型需要重试
@ -88,10 +88,10 @@ public class RetryUtil {
* 没有返回值重试执行方法 * 没有返回值重试执行方法
* *
* @param run 执行方法 * @param run 执行方法
* @param maxAttempts 最大的重试次数 * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次
* @param delay 重试间隔 * @param delay 重试间隔
* @param recover 达到最大重试次数后执行的备用方法 * @param recover 达到最大重试次数后执行的备用方法
* @param predicate 自定义重试条件 * @param predicate 自定义重试条件, 返回true时表示重试
*/ */
public static void ofPredicate(final Runnable run, final long maxAttempts, final Duration delay, public static void ofPredicate(final Runnable run, final long maxAttempts, final Duration delay,
final Supplier<Void> recover, final BiPredicate<Void, Throwable> predicate) { final Supplier<Void> recover, final BiPredicate<Void, Throwable> predicate) {
@ -105,14 +105,14 @@ public class RetryUtil {
/** /**
* 根据异常信息进行重试 * 根据自定义结果进行重试
* 有返回值重试执行方法 * 有返回值重试执行方法
* *
* @param sup 执行方法 * @param sup 执行方法
* @param maxAttempts 最大的重试次数 * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次
* @param delay 重试间隔 * @param delay 重试间隔
* @param recover 达到最大重试次数后执行的备用方法 * @param recover 达到最大重试次数后执行的备用方法
* @param predicate 自定义重试条件 * @param predicate 自定义重试条件, 返回true时表示重试
* @param <T> 结果类型 * @param <T> 结果类型
* @return 执行结果 * @return 执行结果
*/ */

View File

@ -109,11 +109,11 @@ public class RetryableTask<T> {
*/ */
private T result; private T result;
/** /**
* 执行方法 * 执行方法
*/ */
private final Supplier<T> sup; private final Supplier<T> sup;
/** /**
* 重试策略 * 重试策略, 返回true时表示重试
*/ */
private final BiPredicate<T, Throwable> predicate; private final BiPredicate<T, Throwable> predicate;
/** /**
@ -223,7 +223,8 @@ public class RetryableTask<T> {
private RetryableTask<T> doExecute() { private RetryableTask<T> doExecute() {
Throwable th = null; Throwable th = null;
while (--this.maxAttempts >= 0) { // 任务至少被执行一次
do {
try { try {
this.result = this.sup.get(); this.result = this.sup.get();
} catch (final Throwable t) { } catch (final Throwable t) {
@ -236,8 +237,11 @@ public class RetryableTask<T> {
break; break;
} }
ThreadUtil.sleep(delay.toMillis()); // 避免最后一次任务执行时的线程睡眠
} if (this.maxAttempts > 0) {
ThreadUtil.sleep(delay.toMillis());
}
} while (--this.maxAttempts >= 0);
this.throwable = th; this.throwable = th;
return this; return this;