zhub-client/src/main/java/dev/zhub/timer/Timers.java
2024-04-24 20:05:12 +08:00

46 lines
1.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dev.zhub.timer;
import dev.zhub.timer.scheduled.ScheduledCycle;
import org.redkale.util.Utility;
import java.util.function.Supplier;
public class Timers {
private static TimerExecutor timerExecutor = new TimerExecutor(1);
/**
* 本地延时重试
* @param supplier
* @param millis
* @param maxCount
*/
public static void tryDelay(Supplier<Boolean> supplier, long millis, int maxCount) {
timerExecutor.add(TimerTask.by("try-delay-task-" + Utility.uuid(), ScheduledCycle.of(0), task -> {
if (supplier.get() || task.getExecCount() == maxCount) {
task.setComplete(true);
}
if (task.getExecCount() == 1) {
task.setScheduled(ScheduledCycle.of(millis));
}
}));
}
/**
* 本地延时:延时时间极短的场景下使用 1分钟内
* @param runnable
* @param millis
*/
public static void delay(Runnable runnable, long millis) {
timerExecutor.add(TimerTask.by("delay-task-" + Utility.uuid(), ScheduledCycle.of(millis), task -> {
runnable.run();
task.setComplete(true);
}));
}
}