Package org.apache.sis.internal.system
Class DelayedExecutor
java.lang.Object
java.lang.Thread
org.apache.sis.internal.system.DaemonThread
org.apache.sis.internal.system.DelayedExecutor
- All Implemented Interfaces:
Runnable
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.
Comparison with
We tried to use
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),
CacheTest.stress()
tests suggests that the lightweight solution is faster.- Since:
- 0.3
- Version:
- 0.7
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final BlockingQueue
<DelayedRunnable> List of delayed tasks to execute.Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
DelayedExecutor
(DaemonThread lastCreatedDaemon) Constructs a new thread as a daemon thread. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
Returnstrue
if this thread seems to be stalled.final void
run()
Loop to be run during the virtual machine lifetime.static void
schedule
(DelayedRunnable task) Schedules the given short task for later execution in a daemon thread.Methods inherited from class org.apache.sis.internal.system.DaemonThread
isKillRequested, killAll, listStalledThreads
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Field Details
-
QUEUE
List of delayed tasks to execute.
-
-
Constructor Details
-
DelayedExecutor
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 newDelayedRunnable
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
Schedules the given short task for later execution in a daemon thread. The task will be executed after the delay specified byDelayedRunnable.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 interfaceRunnable
- Specified by:
run
in classDaemonThread
-
isStalled
protected boolean isStalled()Returnstrue
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 classDaemonThread
- Returns:
true
if this thread seems to be stalled.
-