@Override public void run() { try { // Note: do not use Thread.interrupted() because it will clear the interrupt // status of the thread. while (!Thread.currentThread().isInterrupted()) { try { DelayedTask task = queue.take(); stats.incrementActiveTaskCount(); final long delay = clock.wallTime() - task.getNextExecutionTime(); stats.taskExecutionDelay().record(delay, TimeUnit.MILLISECONDS); stats.taskExecutionTime().record(() -> task.runAndReschedule(queue, stats)); } catch (InterruptedException e) { LOGGER.debug("task interrupted", e); break; } finally { stats.decrementActiveTaskCount(); } } } finally { startThreads(); } } }
@Override public void run() { try { // Note: do not use Thread.interrupted() because it will clear the interrupt // status of the thread. while (!Thread.currentThread().isInterrupted()) { try { DelayedTask task = queue.take(); stats.incrementActiveTaskCount(); final long delay = clock.wallTime() - task.getNextExecutionTime(); stats.taskExecutionDelay().record(delay, TimeUnit.MILLISECONDS); stats.taskExecutionTime().record(() -> task.runAndReschedule(queue, stats)); } catch (InterruptedException e) { LOGGER.debug("task interrupted", e); break; } finally { stats.decrementActiveTaskCount(); } } } finally { startThreads(); } } }
@Test public void updateNextFixedDelay() { ManualClock clock = new ManualClock(); Registry registry = new DefaultRegistry(clock); Counter skipped = registry.counter("skipped"); Scheduler.Options options = new Scheduler.Options() .withFrequency(Scheduler.Policy.FIXED_DELAY, Duration.ofSeconds(10)); clock.setWallTime(5437L); Scheduler.DelayedTask task = new Scheduler.DelayedTask(clock, options, () -> {}); Assertions.assertEquals(5437L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); clock.setWallTime(12123L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(22123L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); clock.setWallTime(27000L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(37000L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); }
@Test public void updateNextFixedRateSkip() { ManualClock clock = new ManualClock(); Registry registry = new DefaultRegistry(clock); Counter skipped = registry.counter("skipped"); Scheduler.Options options = new Scheduler.Options() .withFrequency(Scheduler.Policy.FIXED_RATE_SKIP_IF_LONG, Duration.ofSeconds(10)); clock.setWallTime(5437L); Scheduler.DelayedTask task = new Scheduler.DelayedTask(clock, options, () -> {}); Assertions.assertEquals(5437L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); clock.setWallTime(12123L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(15437L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); clock.setWallTime(27000L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(35437L, task.getNextExecutionTime()); Assertions.assertEquals(1L, skipped.count()); clock.setWallTime(57000L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(65437L, task.getNextExecutionTime()); Assertions.assertEquals(3L, skipped.count()); }