Class DelayedExecutor

java.lang.Object
java.lang.Thread
org.apache.sis.internal.system.DaemonThread
org.apache.sis.internal.system.DelayedExecutor
All Implemented Interfaces:
Runnable

public final class DelayedExecutor extends DaemonThread
A thread executing short tasks after some (potentially zero nanosecond) delay. This class should be reserved to internal SIS usage without user's code. In practice some user code may be indirectly executed through SIS tasks invoking overrideable methods. But all submitted tasks shall be very quick, since there is only one thread shared by everyone.

The methods for use in this class are:

Comparison with java.util.concurrent

We tried to use ScheduledThreadPoolExecutor in a previous SIS version, but its "fixed-sized pool" design forces us to use only one thread if we do not want to waste resources (profiling shows that even a single thread has very low activity), which reduces the interest of that class. Combination of ThreadPoolExecutor super-class with DelayedQueue were not successful neither.

Given that:

  • it seems difficult to configure (Scheduled)ThreadPoolExecutor in such a way that two or more threads are created only when really needed,
  • using those executor services seems an overkill when the pool size is fixed to one thread,
  • our profiling has show very low activity for that single thread anyway,
  • we do not need cancellation and shutdown services for house keeping tasks (this is a daemon thread),
a more lightweight solution seems acceptable here. Pseudo-benchmarking using the CacheTest.stress() tests suggests that the lightweight solution is faster.
Since:
0.3
Version:
0.7
See Also:
  • Field Details

  • Constructor Details

    • DelayedExecutor

      private DelayedExecutor(DaemonThread lastCreatedDaemon)
      Constructs a new thread as a daemon thread. This thread will be sleeping most of the time. It will run only only a few nanoseconds every time a new DelayedRunnable is taken.
      Note: We give to this thread a priority higher than the normal one since this thread shall execute only tasks to be completed very shortly. Quick execution of those tasks is at the benefit of the rest of the system, since they make more resources available sooner.
  • Method Details

    • schedule

      public static void schedule(DelayedRunnable task)
      Schedules the given short task for later execution in a daemon thread. The task will be executed after the delay specified by DelayedRunnable.getDelay(TimeUnit) The task must completes quickly, because we will typically use only one thread for all submitted tasks. Completion of the task shall not be of critical importance, because the JVM is allowed to shutdown before task completion.
      Parameters:
      task - the task to schedule for later execution.
    • run

      public final void run()
      Loop to be run during the virtual machine lifetime. Public as an implementation side-effect; do not invoke explicitly!
      Specified by:
      run in interface Runnable
      Specified by:
      run in class DaemonThread
    • isStalled

      protected boolean isStalled()
      Returns true if this thread seems to be stalled. This method checks the head of the queue. If the delay for that head has expired and the head is not removed in the next 5 seconds, then we will presume that the thread is stalled or dead.
      Overrides:
      isStalled in class DaemonThread
      Returns:
      true if this thread seems to be stalled.