Class DelayedRunnable

java.lang.Object
org.apache.sis.internal.system.DelayedRunnable
All Implemented Interfaces:
Comparable<Delayed>, Runnable, Delayed
Direct Known Subclasses:
ConcurrentAuthorityFactory.CloseTask, DelayedRunnable.Immediate, MetadataSource.CloseTask

public abstract class DelayedRunnable extends Object implements Delayed, Runnable
A task to be submitted to DelayedExecutor for later execution.
Design note: we considered removing this interface in favor a library-wide executor instead of DelayedExecutor. But benchmarks suggested that this lightweight approach for the specific needs of SIS is preferable. See SIS-76 for more information.
Since:
0.3
Version:
1.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A "delayed" task which is actually executed as soon as possible.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) final long
    Time of execution of this task, in nanoseconds provided by System.nanoTime().
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    DelayedRunnable(int delay, TimeUnit unit)
    Creates a new task to be executed after the given delay.
    protected
    DelayedRunnable(long timestamp)
    Creates a new task to be executed at the given time.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Compares this task with the given delayed object for ordering.
    long
    Returns the delay to wait before to execute this task, or 0 if this task shall be executed immediately.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Runnable

    run
  • Field Details

    • timestamp

      final long timestamp
      Time of execution of this task, in nanoseconds provided by System.nanoTime(). In the particular case of the DelayedRunnable.Immediate subclass, the meaning of this field is modified: it is rather an ordinal value used for preserving task order.
      Note: we use System.nanoTime() instead of System.currentTimeMillis() because the latter is not guaranteed to be monotonic: currentTimeMillis may change abruptly for example if the user adjusts the clock of his operating system.
  • Constructor Details

    • DelayedRunnable

      protected DelayedRunnable(int delay, TimeUnit unit)
      Creates a new task to be executed after the given delay.
      Parameters:
      delay - delay before execution of this task.
      unit - unit of measurement of given delay.
    • DelayedRunnable

      protected DelayedRunnable(long timestamp)
      Creates a new task to be executed at the given time. It is user's responsibility to add the System.nanoTime() value to the delay (s)he wants to wait.
      Parameters:
      timestamp - time of execution of this task, in nanoseconds relative to System.nanoTime().
  • Method Details

    • getDelay

      public long getDelay(TimeUnit unit)
      Returns the delay to wait before to execute this task, or 0 if this task shall be executed immediately.
      Specified by:
      getDelay in interface Delayed
    • compareTo

      public int compareTo(Delayed other)
      Compares this task with the given delayed object for ordering. The other object shall be an instance of DelayedRunnable. This restriction should be okay since the DelayedExecutor queue accepts only DelayedRunnable instances.
      Specified by:
      compareTo in interface Comparable<Delayed>
      Parameters:
      other - the other delayed object to compare with this delayed task.
      Returns:
      -1 if the other task should happen before this one, +1 if it should happen after, or 0.