All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.erasmuswithoutpaper.registryclient.SelfSchedulableTask Maven / Gradle / Ivy

There is a newer version: 1.10.0
Show newest version
package eu.erasmuswithoutpaper.registryclient;

import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * This is a simple {@link Runnable} wrapper that allows it to dynamically schedule a next run of
 * itself (with help of the provided executor).
 */
abstract class SelfSchedulableTask implements Runnable {

  private final ScheduledExecutorService executor;
  private final long minPeriod;

  /**
   * Create a new task.
   *
   * 

* Please note, that this constructor will not schedule the first run. You will need to do * it yourself. *

* * @param executor an {@link ScheduledExecutorService} to use when scheduling subsequent runs of * this task. * @param minPeriod the minimum allowed period between runs, in milliseconds. If the {@link Date} * returned by {@link #runAndScheduleNext()} will be earlier than this, then a later date * will be used in its place (so that it fits the limit specified here). */ SelfSchedulableTask(ScheduledExecutorService executor, long minPeriod) { this.executor = executor; this.minPeriod = minPeriod; } @Override public final void run() { Date nextRun = this.runAndScheduleNext(); if (nextRun == null) { return; } long delay = nextRun.getTime() - System.currentTimeMillis(); if (delay < this.minPeriod) { delay = this.minPeriod; } this.executor.schedule(this, delay, TimeUnit.MILLISECONDS); } /** * Run the task and decide when it should be run next time. * * @return Either a {@link Date} in the future, or null if no subsequent runs should be * scheduled. */ protected abstract Date runAndScheduleNext(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy