Package org.apache.sis.internal.system
Class DelayedExecutor
java.lang.Object
java.lang.Thread
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)ThreadPoolExecutorin 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
- Author:
- Martin Desruisseaux (Geomatys)
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Method Summary
Modifier and TypeMethodDescriptionprotected final booleanReturnstrueif this daemon thread shall terminate.protected booleanReturnstrueif this thread seems to be stalled.final voidrun()Loop to be run during the virtual machine lifetime.static voidschedule(DelayedRunnable task) Schedules the given short task for later execution in a daemon thread.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
-
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! -
isStalled
protected boolean isStalled()Returnstrueif 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.- Returns:
trueif this thread seems to be stalled.
-
isKillRequested
protected final boolean isKillRequested()Returnstrueif this daemon thread shall terminate. This happen at shutdown time.- Returns:
trueif this daemon thread shall terminate.
-