• 0.22.6

Schedule With Fixed Delay without Executor or Thread Manipulations

Make your class implement Runnable (or Callable) and annotate it with @ScheduleWithFixedDelay annotation:

@ScheduleWithFixedDelay(delay = 1, unit = TimeUnit.SECONDS)
public class Clock implements Runnable, Closeable {
  @Override
  public void run() {
    System.out.println("tick tick..");
  }
  @Override
  public void close() {
    System.out.println("done with ticking");
  }
}

Now, make an instance of this class and it will start ticking immediately:

public class Main {
  public static void main(String[] args) {
    Clock clock = new Clock();
    TimeUnit.SECONDS.sleep(5); // Wait for five seconds.
    clock.close();
  }
}

If your class implements Closeable ,you can stop a routine thread by calling its close() method.

NOTE: You can only schedule one execution between all equal objects (i.e. instances that are equal according to equals() ). Invoking the same method multiple times and across equal objects, without stopping the scheduled execution beforehand, will result in an IllegalStateException being thrown. This is in order to prevent bugs that result from "stray" executions that were scheduled by mistake.

The mechanism is implemented with AOP/AspectJ. Read to know how to integrate it into your pom.xml.