/** * The fixed sleep time. * * @param sleepTime The sleep time. * @param timeUnit The time unit. * @param <V> The return value type. * @return The wait strategy action. */ public static <V> Action1<TaskContext<V>> fixedWait(long sleepTime, TimeUnit timeUnit) { return ctx -> ThreadUtils.sleep(sleepTime, timeUnit); }
/** * The fixed sleep time. * * @param sleepTime The sleep time. * @param timeUnit The time unit. * @param <V> The return value type. * @return The wait strategy action. */ public static <V> Action1<TaskContext<V>> fixedWait(long sleepTime, TimeUnit timeUnit) { return ctx -> ThreadUtils.sleep(sleepTime, timeUnit); }
static void sleep(long millis) { ThreadUtils.sleep(millis); }
static void sleep(long sleepTime, TimeUnit timeUnit) { ThreadUtils.sleep(sleepTime, timeUnit); } }
static void sleep(long sleepTime, TimeUnit timeUnit) { ThreadUtils.sleep(sleepTime, timeUnit); } }
static void sleep(long millis) { ThreadUtils.sleep(millis); }
@Override public void run() { while (start) { int currentSlotTemp = currentSlot; ConcurrentLinkedQueue<TimerTask> timerSlot = timerSlots[currentSlotTemp++]; currentSlotTemp %= timerSlots.length; timerSlot.removeIf(TimerTask::runTask); ThreadUtils.sleep(interval); currentSlot = currentSlotTemp; } }
@Override public void run() { while (start) { int currentSlotTemp = currentSlot; ConcurrentLinkedQueue<TimerTask> timerSlot = timerSlots[currentSlotTemp++]; currentSlotTemp %= timerSlots.length; timerSlot.removeIf(TimerTask::runTask); ThreadUtils.sleep(interval); currentSlot = currentSlotTemp; } }
/** * The exponential increase sleep time. The formula: initTime * 2 ^ ((count - 1) * (multiple - 1)). * * @param initTime The first sleep time. * @param maxTime The max sleep time. * @param timeUnit The time unit. * @param multiple The growth factor. * @param <V> The return value type. * @return The wait strategy action. */ public static <V> Action1<TaskContext<V>> exponentialWait(long initTime, long maxTime, TimeUnit timeUnit, int multiple) { Assert.isTrue(multiple > 0, "The multiple must be great than 0"); Assert.isTrue(maxTime >= initTime, "The max time must be great than or equals init time"); return ctx -> { int count = Math.max(ctx.getExecutedCount(), 1) - 1; long sleepTime = initTime << (count * (multiple - 1)); ThreadUtils.sleep(Math.min(sleepTime, maxTime), timeUnit); }; }
/** * The exponential increase sleep time. The formula: initTime * 2 ^ ((count - 1) * (multiple - 1)). * * @param initTime The first sleep time. * @param maxTime The max sleep time. * @param timeUnit The time unit. * @param multiple The growth factor. * @param <V> The return value type. * @return The wait strategy action. */ public static <V> Action1<TaskContext<V>> exponentialWait(long initTime, long maxTime, TimeUnit timeUnit, int multiple) { Assert.isTrue(multiple > 0, "The multiple must be great than 0"); Assert.isTrue(maxTime >= initTime, "The max time must be great than or equals init time"); return ctx -> { int count = Math.max(ctx.getExecutedCount(), 1) - 1; long sleepTime = initTime << (count * (multiple - 1)); ThreadUtils.sleep(Math.min(sleepTime, maxTime), timeUnit); }; }