Class Interval


  • @ThreadSafe
    public class Interval
    extends Object
    Helper class to allow for something to happen at regular intervals. A common use case might be invalidation of in-memory data.

    Example to invalidate a cache every minute:
     
     private final Interval cacheInvalidationInterval = new Interval(Duration.ofMinutes(1));
    
     ...
    
     cacheInvalidationInterval.runIfDue(this::invalidateCache);
     
     
    • Constructor Detail

      • Interval

        public Interval​(Duration duration)
    • Method Detail

      • runIfDue

        public boolean runIfDue​(Runnable runnable)
        Executes the runnable if the interval is due or past due. The next full interval is started after completion of the runnable.
        Returns:
        true if the operation was due and the runnable completed.