Class AbstractObjectSpecificExecutor<O1,O2,R>
- Type Parameters:
O1- The type of objects for which runnables are registered.O2- The type of objects with which runnables are managed and executed within this executor.R- The runtime data for managed objects. This allows for arbitrary data for each managed object.
- All Implemented Interfaces:
Executor,ExecutorService
- Direct Known Subclasses:
AbstractObjectOneWorkExecutor,AbstractPipeliningExecutor
ThreadPoolExecutor except that it rejects the
work as soon as a shutdown has been requested. This executor provides the means to flush, that is, wait for pending work or abort pending work, execute a specific runnable and continue with new work arriving after the flush. Therefore flush synchronises the work of all objects.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe task of a flush executing anObjectSpecificFlushCallable.protected static final classA handler for rejected executions which signals the rejection to the affected object runnable and delegates to the actual handler otherwise.Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final AtomicIntegerA counter for all flushs that aborted pending work.protected final AtomicIntegerA counter for pending flushs.protected final LoggerThe logger for log messages, especially runtime exceptions occurring when executing work.All managed objects, their runnables as well as their runtime data.protected final AtomicBooleanWhether a shutdown has been requested.protected static final intThe amount of dedicated threads executing flush tasks concurrently which makes an executor without room for flush tasks within the pool warn about the concurrent flushing.Fields inherited from class de.aristaflow.adept2.util.threading.CachedThreadPoolExecutor
coreSizeSync, fullSize, realCoreSize, realMaxSize, workSizeFields inherited from class de.aristaflow.adept2.util.threading.AFThreadPoolExecutor
activeThreads, completedTasks, HYSTERESIS, submittedTasks -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, boolean prestart, Adept2ThreadFactory threadFactory) Deprecated, for removal: This API element is subject to removal in a future version.protectedAbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, boolean prestart, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics) Deprecated, for removal: This API element is subject to removal in a future version.protectedAbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, boolean prestart, Comparator<? super Runnable> comparator, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics) Deprecated, for removal: This API element is subject to removal in a future version.protectedAbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, boolean prestart, Comparator<? super Runnable> comparator, Adept2ThreadFactory threadFactory, Level minLogLevelPoolStatistics) Deprecated, for removal: This API element is subject to removal in a future version.protectedAbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, Adept2ThreadFactory threadFactory) Creates a new thread pool executor with the designated number of threads and threads for different objects using the designate thread factory for creating threads.protectedAbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics) Creates a new thread pool executor with the designated number of threads and threads for different objects using the designate thread factory for creating threads.protectedAbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, Comparator<? super Runnable> comparator, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics) Creates a new thread pool executor with the designated (maximum) amount of (core) threads using the designated comparator for prioritising work.protectedAbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, Comparator<? super Runnable> comparator, Adept2ThreadFactory threadFactory, Level minLogLevelPoolStatistics) Creates a new thread pool executor with the designated (maximum) amount of (core) threads using the designated comparator for prioritising work.protectedAbstractObjectSpecificExecutor(int maxPoolSize, Adept2ThreadFactory threadFactory) Creates a new thread pool executor with the designated maximum amount of threads and threads for different objects.protectedAbstractObjectSpecificExecutor(Adept2ThreadFactory threadFactory) Deprecated, for removal: This API element is subject to removal in a future version. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ObjectRunnable<O1,O2> addRunnable(O1 registeredObject, O2 indexObject, Runnable task) Adds the designated runnable for the designated object and index object to this executor.booleanawaitTermination(long timeout, TimeUnit unit) This also awaits the termination of the dedicated threads executing flush tasks which exceeded the limit of flush tasks within the pool.cancelAndFlush(boolean shutdownRequest, Map<O2, R> pending, Callable<T> task, Collection<ObjectRunnable<O1, O2>> objectRunnables) Cancels the designated pending work (if appropriate), flushes (i.protected static voidcancelWork(Runnable work, boolean mayInterrupt, Logger logger, String description, Object... descriptionArgs) Cancels the designated work if it is aRunnableFuture.protected abstract voidclearAbortedPendingWork(boolean shutdownRequest) Removes all pending work that should be aborted for an aborting flush, that is, it removes the from the data structures, e.voidThe submitted tasks and the stack of the caller are accounted for before the work is handed to the pool, and their counterparts take place inAFThreadPoolExecutor.beforeExecute(Thread, Runnable)and inAFThreadPoolExecutor.afterExecute(Runnable, Throwable).voidExecutes the designated task for the designated object.protected abstract voidexecuteShutdown(Runnable shutdown) Executes the designated shutdown task which contains a flush callingThreadPoolExecutor.shutdown()without abortion.protected voidexecuteUnchecked(Runnable command) The same asexecute(Runnable)but without the check for a pending shutdown.flushAndExecute(boolean abortPending, Callable<T> task) Flushes all work and executes the designated task.flushAndExecuteUnchecked(boolean abortPending, boolean shutdownRequest, Callable<T> task) The same asflushAndExecute(boolean, Callable)but without the check for a pending shutdown.protected abstract O2getIndexObjectFor(O1 object) Gets the object used for managing and executing runnables within this executor.getOutstandingRunnables(Map<O2, R> outstandingWork) Gets a list of runnables which have not been executed based on the designated outstanding work.This returns the handler which has been set, not theAbstractObjectSpecificExecutor.SignallingRejectionHandlerwrapping it.booleanReturns whether this executor has been shut down, independently of whether there is still work pending.protected booleanrestart(boolean abortedPending) Restarts the work after a flush.protected voidActually restarts by inspecting all existing objects for whether there is still a runnable for it.voidThe designated handler is wrapped in aAbstractObjectSpecificExecutor.SignallingRejectionHandlerwhich signals a swallowed task to the affected object runnable.voidshutdown()Shutting down will reject new work (normal or flushing).This shutdown will reject new work (normal or flushing) and abort the current work: the pending work is cancelled, the work queue of the pool is drained and the threads executing work are interrupted.protected voidThis method is therun()method of the shutdown runnable.Future<?>Submits the designated task for the designated object.<T> Future<T>Submits the designated task for the designated object.submitAndFlush(boolean abortPending, Callable<T> task) Submits the designated task and flushes all work.protected <T> Future<T>submitFlusher(ObjectSpecificFlushCallable<O2, T> flusher) Submits the designated flusher for execution asAbstractObjectSpecificExecutor.FlushTask.protected static RejectedExecutionHandlerUnwraps the designated handler for rejected executions in case it is aAbstractObjectSpecificExecutor.SignallingRejectionHandler.Methods inherited from class de.aristaflow.adept2.util.threading.CachedThreadPoolExecutor
afterExecuteMethods inherited from class de.aristaflow.adept2.util.threading.AFThreadPoolExecutor
beforeExecute, logPoolStatistics, newTaskFor, newTaskFor, recalculateLogLimits, renameThread, setCorePoolSize, setLogExecuteCallStack, setMaximumPoolSize, toString, updateQueueLimitMethods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getTaskCount, getThreadFactory, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setKeepAliveTime, setThreadFactory, terminated
-
Field Details
-
objects
All managed objects, their runnables as well as their runtime data. -
flushPending
A counter for pending flushs. With this running object runnables will be terminated/suspended and concurrent flushs will be synchronised. That is, the threads causing the flush will block until all current pending flushs have been executed.
Access has to be synchronised viaobjects. -
abortCount
A counter for all flushs that aborted pending work.
Access has to be synchronised viaobjects. -
shutdownPending
Whether a shutdown has been requested. -
WARN_DEDICATED_FLUSHERS
protected static final int WARN_DEDICATED_FLUSHERSThe amount of dedicated threads executing flush tasks concurrently which makes an executor without room for flush tasks within the pool warn about the concurrent flushing. A single dedicated thread is the normal case for such an executor, only these threads accumulating indicates concurrent flushing.- See Also:
-
logger
The logger for log messages, especially runtime exceptions occurring when executing work.
-
-
Constructor Details
-
AbstractObjectSpecificExecutor
@Deprecated(since="14.4.0", forRemoval=true) protected AbstractObjectSpecificExecutor(Adept2ThreadFactory threadFactory) Deprecated, for removal: This API element is subject to removal in a future version.Creates a new thread pool executor with an unlimited amount of threads for different objects using the designate thread factory for creating threads. The threads will stay idle for 60 seconds before they terminate.- Parameters:
threadFactory- The factory to use when the executor creates a new thread.
-
AbstractObjectSpecificExecutor
Creates a new thread pool executor with the designated maximum amount of threads and threads for different objects. Threads are created using the designated thread factory. The threads will stay idle for 60 seconds before they terminate, no idle thread will live forever.- Parameters:
maxPoolSize- The maximum number of threads allowed in the pool. Set this to the number of objects allowed to be executed concurrently or less.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.
-
AbstractObjectSpecificExecutor
protected AbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, Adept2ThreadFactory threadFactory) Creates a new thread pool executor with the designated number of threads and threads for different objects using the designate thread factory for creating threads. The threads will stay idle for 60 seconds before they terminate.- Parameters:
corePoolSize- The number of threads to keep in the pool all the time, even if they are idle.maxPoolSize- The maximum number of threads allowed in the pool. Set this to the number of objects allowed to be executed concurrently or less.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.
-
AbstractObjectSpecificExecutor
@Deprecated(since="14.4.0", forRemoval=true) protected AbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, boolean prestart, Adept2ThreadFactory threadFactory) Deprecated, for removal: This API element is subject to removal in a future version.Creates a new thread pool executor with the designated number of threads and threads for different objects using the designate thread factory for creating threads. The threads will stay idle for 60 seconds before they terminate.- Parameters:
corePoolSize- The number of threads to keep in the pool, even if they are idle.maxPoolSize- The maximum number of threads to allow in the pool.prestart- Whether all core threads should be prestarted.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.
-
AbstractObjectSpecificExecutor
protected AbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics) Creates a new thread pool executor with the designated number of threads and threads for different objects using the designate thread factory for creating threads. The threads will stay idle for 60 seconds before they terminate.- Parameters:
corePoolSize- The number of threads to keep in the pool all the time, even if they are idle.maxPoolSize- The maximum number of threads allowed in the pool. Set this to the number of objects allowed to be executed concurrently or less.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.queueLimitPoolStatistics- The amount of tasks in the queue before warnings will be logged. Use-1to use the default limits.minLogLevelPoolStatistics- The log level which need to be reached for logging pool statistics. Pool statistics logs with a lower log level will be ignored.
-
AbstractObjectSpecificExecutor
@Deprecated(since="14.4.0", forRemoval=true) protected AbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, boolean prestart, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics) Deprecated, for removal: This API element is subject to removal in a future version.Creates a new thread pool executor with the designated number of threads and threads for different objects using the designate thread factory for creating threads. The threads will stay idle for 60 seconds before they terminate.- Parameters:
corePoolSize- The number of threads to keep in the pool, even if they are idle.maxPoolSize- The maximum number of threads to allow in the pool.prestart- Whether all core threads should be prestarted.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.queueLimitPoolStatistics- The amount of tasks in the queue before warnings will be logged. Use-1to use the default limits.minLogLevelPoolStatistics- The log level which need to be reached for logging pool statistics. Pool statistics logs with a lower log level will be ignored.
-
AbstractObjectSpecificExecutor
protected AbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, Comparator<? super Runnable> comparator, Adept2ThreadFactory threadFactory, Level minLogLevelPoolStatistics) Creates a new thread pool executor with the designated (maximum) amount of (core) threads using the designated comparator for prioritising work. More work will enqueued. The designated thread factory will be used for creating new threads. The non-core threads will stay idle for 60 seconds before they terminate.- Parameters:
corePoolSize- The number of threads to keep in the pool all the time, even if they are idle.maxPoolSize- The maximum number of threads allowed in the pool. Set this to the number of objects allowed to be executed concurrently or less.comparator- The comparator used to determine the priority of work. It must not order flush tasks before object runnables, see there.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.minLogLevelPoolStatistics- The log level which need to be reached for logging pool statistics. Pool statistics logs with a lower log level will be ignored.
-
AbstractObjectSpecificExecutor
@Deprecated(since="14.4.0", forRemoval=true) protected AbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, boolean prestart, Comparator<? super Runnable> comparator, Adept2ThreadFactory threadFactory, Level minLogLevelPoolStatistics) Deprecated, for removal: This API element is subject to removal in a future version.Creates a new thread pool executor with the designated (maximum) amount of (core) threads using the designated comparator for prioritising work. More work will enqueued. The designated thread factory will be used for creating new threads. The non-core threads will stay idle for 60 seconds before they terminate.- Parameters:
corePoolSize- The number of threads to keep in the pool, even if they are idle.maxPoolSize- The maximum number of threads to allow in the pool.prestart- Whether all core threads should be prestarted.comparator- The comparator used to determine the priority of work. It must not order flush tasks before object runnables, see there.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.minLogLevelPoolStatistics- The log level which need to be reached for logging pool statistics. Pool statistics logs with a lower log level will be ignored.
-
AbstractObjectSpecificExecutor
protected AbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, Comparator<? super Runnable> comparator, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics) Creates a new thread pool executor with the designated (maximum) amount of (core) threads using the designated comparator for prioritising work. More work will enqueued. The designated thread factory will be used for creating new threads. The non-core threads will stay idle for 60 seconds before they terminate.- Parameters:
corePoolSize- The number of threads to keep in the pool all the time, even if they are idle.maxPoolSize- The maximum number of threads allowed in the pool. Set this to the number of objects allowed to be executed concurrently or less.comparator- The comparator used to determine the priority of work. It must not order flush tasks before object runnables, see there.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.queueLimitPoolStatistics- The amount of tasks in the queue before warnings will be logged. Use-1to use the default limits.minLogLevelPoolStatistics- The log level which need to be reached for logging pool statistics. Pool statistics logs with a lower log level will be ignored.
-
AbstractObjectSpecificExecutor
@Deprecated(since="14.4.0", forRemoval=true) protected AbstractObjectSpecificExecutor(int corePoolSize, int maxPoolSize, boolean prestart, Comparator<? super Runnable> comparator, Adept2ThreadFactory threadFactory, int queueLimitPoolStatistics, Level minLogLevelPoolStatistics) Deprecated, for removal: This API element is subject to removal in a future version.Creates a new thread pool executor with the designated (maximum) amount of (core) threads using the designated comparator for prioritising work. More work will enqueued. The designated thread factory will be used for creating new threads. The non-core threads will stay idle for 60 seconds before they terminate.- Parameters:
corePoolSize- The number of threads to keep in the pool, even if they are idle.maxPoolSize- The maximum number of threads to allow in the pool.prestart- Whether all core threads should be prestarted.comparator- The comparator used to determine the priority of work. It must not order flush tasks before object runnables, see there.threadFactory- The factory to use when the executor creates a new thread. Also the logger from this thread factory will be used by this executor.queueLimitPoolStatistics- The amount of tasks in the queue before warnings will be logged. Use-1to use the default limits.minLogLevelPoolStatistics- The log level which need to be reached for logging pool statistics. Pool statistics logs with a lower log level will be ignored.
-
-
Method Details
-
execute
Executes the designated task for the designated object. This just creates the index object, adds the runnable and executes it if not terminated or waiting for a flush.
How the runnable and the objects are handled depends on the actual implementation.- Parameters:
object- The object for which to execute the designated task.task- The task which to execute.- Throws:
RejectedExecutionException- if the executor has been shut down, aRejectedExecutionExceptionwill be thrown.- See Also:
-
execute
The submitted tasks and the stack of the caller are accounted for before the work is handed to the pool, and their counterparts take place inAFThreadPoolExecutor.beforeExecute(Thread, Runnable)and inAFThreadPoolExecutor.afterExecute(Runnable, Throwable). Neither of those is reached for work the pool does not accept, so the accounting is compensated where handing the work over fails. This rejects the work as soon as a shutdown has been requested, just likeexecute(Object, Runnable)does. The inheritedsubmitandinvokeAll/invokeAnymethods delegate to this method and therefore reject as well.- Specified by:
executein interfaceExecutor- Overrides:
executein classCachedThreadPoolExecutor- Throws:
RejectedExecutionException- If the executor has been shut down, aRejectedExecutionExceptionwill be thrown.
-
executeUnchecked
The same asexecute(Runnable)but without the check for a pending shutdown. This is required for the shutdown procedure as well as for restarting work which a flush has suppressed: both take place when a shutdown has already been requested.An object runnable is counted as submitted from being handed to the pool on, so awaiting its termination awaits it even before it has started: work which has been submitted before a flush completes before the task of the flush and no work runs concurrently to this task. A rejected execution reverts the counting.
- Parameters:
command- The work which to execute without checking for a pending shutdown.
-
setRejectedExecutionHandler
The designated handler is wrapped in aAbstractObjectSpecificExecutor.SignallingRejectionHandlerwhich signals a swallowed task to the affected object runnable.getRejectedExecutionHandler()returns the designated handler itself again, the wrapper stays invisible.- Overrides:
setRejectedExecutionHandlerin classThreadPoolExecutor
-
getRejectedExecutionHandler
This returns the handler which has been set, not theAbstractObjectSpecificExecutor.SignallingRejectionHandlerwrapping it.- Overrides:
getRejectedExecutionHandlerin classThreadPoolExecutor
-
unwrapRejectionHandler
Unwraps the designated handler for rejected executions in case it is aAbstractObjectSpecificExecutor.SignallingRejectionHandler. This keeps the wrapper invisible to the outside and avoids wrapping a wrapper again.- Parameters:
handler- The handler for rejected executions which to unwrap.- Returns:
- The actual handler for rejected executions, that is, the designated handler itself or the handler to which it delegates.
-
cancelWork
protected static void cancelWork(Runnable work, boolean mayInterrupt, Logger logger, String description, Object... descriptionArgs) Cancels the designated work if it is aRunnableFuture. A cancellation will be logged at FINER, work which cannot be cancelled will be logged at FINEST, both with the designated description of the work.- Parameters:
work- The work which to cancel. If this is noRunnableFuture(ornull), it cannot be cancelled and will only be logged.mayInterrupt- Whether the thread executing the work may be interrupted.logger- The logger for the progress of the cancellation.description- The description of the work for the logging. This is a format string with indexed specifiers for the designated arguments; it has to fit both, "Cancelled <description>." and "Cannot cancel <description>, …".descriptionArgs- The arguments for the designated description.
-
getIndexObjectFor
Gets the object used for managing and executing runnables within this executor.- Parameters:
object- The object for which a runnable is registered.- Returns:
- The object used for managing and executing runnables (registered for the designated object) within this executor.
-
addRunnable
protected abstract ObjectRunnable<O1,O2> addRunnable(O1 registeredObject, O2 indexObject, Runnable task) Adds the designated runnable for the designated object and index object to this executor.
Implementations may store the work internally for later execution or assign it to an already existing active element (usually a thread). If they need a new active element for the designated runnable, they have to return a new runnable which will then be added to this executor using the normalexecuteUnchecked(Runnable). If they returnnull, they have to assign the runnable to an active element eventually and arbitrarily.The caller has the lock on
objects.- Parameters:
registeredObject- The object for which this runnable has been registered.indexObject- The object with which this runnable is managed and executed within the corresponding object-specific executor service.task- The task which to execute.- Returns:
- A runnable which to add to the executor so that this gets a new active element
(thread). This will be handled via
executeUnchecked(Runnable), which means the runnable will be executed independently from other work and a new thread may be started for it.
-
flushAndExecute
Flushes all work and executes the designated task. When aborting, all registered work will be removed and returned. Otherwisenullwill be returned and the object work will restart after the designated task has been executed.
This method can be called concurrently. It is ensured that all concurrent flushs will be executed before the object work will restart. The tasks of concurrent flushs will be executed concurrently! Each flush occupies a thread until the awaited work has terminated and its task has been executed;submitFlusher(ObjectSpecificFlushCallable)describes how these threads are provided.- Parameters:
abortPending- Whether the present work should be cancelled. In this case the corresponding runtime data will be returned.task- The task which to execute without any object work running.- Returns:
- A pair containing the future representing the designated task and as second element
either
nullin case no abortion is requested otherwise the managed objects and the runtime data of the aborted work. - Throws:
RejectedExecutionException- if the executor has been shut down, aRejectedExecutionExceptionwill be thrown.
-
flushAndExecuteUnchecked
protected <T> Pair<Future<T>,Map<O2, flushAndExecuteUncheckedR>> (boolean abortPending, boolean shutdownRequest, Callable<T> task) The same asflushAndExecute(boolean, Callable)but without the check for a pending shutdown. This is required for the shutdown procedure.- Parameters:
abortPending- Whether the present work should be cancelled. In this case the corresponding runtime data will be returned.shutdownRequest- Whether this flush is caused by ashutdownNow(). In this case all work has to be aborted, even the work pending after a flush.task- The task which to execute without any object work running.- Returns:
- A pair containing the future representing the designated task and as second element
either
nullin case no abortion is requested otherwise the managed objects and the runtime data of the aborted work.
-
clearAbortedPendingWork
protected abstract void clearAbortedPendingWork(boolean shutdownRequest) Removes all pending work that should be aborted for an aborting flush, that is, it removes the from the data structures, e. g.objects. This method only affects data structures, it does not affect any executed work. The caller will take care of this shortly.The caller has the lock on
objects.- Parameters:
shutdownRequest- Whether this flush is caused by ashutdownNow(). In this case all work has to be aborted, even the work pending after a flush.
-
cancelAndFlush
protected abstract <T> Pair<Future<T>,Map<O2, cancelAndFlushR>> (boolean shutdownRequest, Map<O2, R> pending, Callable<T> task, Collection<ObjectRunnable<O1, O2>> objectRunnables) Cancels the designated pending work (if appropriate), flushes (i. e. waits for or aborts the designated runnables) and executes the designated task.The caller has the lock on
objects.- Parameters:
shutdownRequest- Whether this flush is caused by ashutdownNow(). In this case all work has to be aborted, even the work pending after a flush.pending- The pending work that will not be executed due to abort ornullif running work should not be aborted.task- The task which to execute without any object work running.objectRunnables- The currently running work. This will either be aborted or awaited for flushing.- Returns:
- A pair containing the future representing the designated task and as second element
either
nullin case no abortion is requested otherwise the managed objects and the runtime data of the aborted work. Note that the runtime data may contain work that is not cancelled by the flush. This depends on the runtime data.
-
submitFlusher
Submits the designated flusher for execution asAbstractObjectSpecificExecutor.FlushTask. A flush task blocks its thread until the awaited work has terminated, and work which has been submitted but has not started yet needs a thread of the pool to terminate. Therefore at most the maximum pool size minus one flush tasks are admitted to the pool itself, so at least one thread always remains for executing work. Every flush task exceeding this limit is executed by a dedicated new thread: concurrent flushing works but drains resources, so this will be logged as warning once, all further dedicated threads will be logged at a lower level. The warning may be deferred in case of a single-thread executor (WARN_DEDICATED_FLUSHERS).- Type Parameters:
T- The type of result of the designated flusher.- Parameters:
flusher- The flusher which to execute.- Returns:
- The flush task executing the designated flusher.
- Throws:
RejectedExecutionException- If the executor has been shut down, aRejectedExecutionExceptionwill be thrown.
-
submit
Submits the designated task for the designated object.- Parameters:
object- The object for which to execute the designated task.task- The task which to execute.- Returns:
- A
Futurerepresenting pending completion of the task - Throws:
RejectedExecutionException- if the executor has been shut down, aRejectedExecutionExceptionwill be thrown.- See Also:
-
submit
Submits the designated task for the designated object.- Parameters:
object- The object for which to execute the designated task.task- The task which to execute.- Returns:
- A
Futurerepresenting pending completion of the task - Throws:
RejectedExecutionException- if the executor has been shut down, aRejectedExecutionExceptionwill be thrown.- See Also:
-
submitAndFlush
Submits the designated task and flushes all work. When aborting, all registered work will be removed and returned. Otherwisenullwill be returned and the work will restart after the designated task has been executed.
This method can be called concurrently. It is ensured that all concurrent flushs will be executed before the work will restart. The tasks of concurrent flushs will be executed concurrently!- Parameters:
abortPending- Whether the present work should be cancelled. In this case the corresponding runnables will be returned.task- The task which to execute without any object work running.- Returns:
- A pair containing the future representing the designated task and as second element
either
nullin case no abortion is requested otherwise the managed objects and the runtime data of the aborted work. - Throws:
RejectedExecutionException- if the executor has been shut down.- See Also:
-
restart
protected boolean restart(boolean abortedPending) Restarts the work after a flush. If there are several concurrent flushs the work will be started after the last flush.- Parameters:
abortedPending- Whether the flush leading to this restart has aborted pending work.- Returns:
- Whether the work has been restarted. Otherwise there are concurrent flushs or the executor has been shut down.
-
restartPendingWork
protected void restartPendingWork()Actually restarts by inspecting all existing objects for whether there is still a runnable for it. If so, this runnable will be executed, that is, the work will be added to the thread pool. If there is no such runnable, the object will be removed.The caller has the lock on
objects. -
shutdown
public void shutdown()Shutting down will reject new work (normal or flushing). But the current work may terminate. If there is currently a flush pending, the actual shutdown will wait until the suppressed work has been executed.This deviates from
ExecutorService.shutdown(): the pool itself is shut down by a flush and therefore only after the outstanding work has terminated, so it still accepts the internal submissions the shutdown depends on whileisShutdown()already reports the requested shutdown. Work which does not terminate therefore keeps the pool alive, which is whatshutdownNow()escalates. Shutting down again gracefully does nothing.- Specified by:
shutdownin interfaceExecutorService- Overrides:
shutdownin classThreadPoolExecutor
-
executeShutdown
Executes the designated shutdown task which contains a flush callingThreadPoolExecutor.shutdown()without abortion.
This method is only called if there is pending work. So the implementation may decide on how and when to execute the shutdown runnable.The caller has the lock on
objects.- Parameters:
shutdown- The task which to perform to shutdown.
-
shutdownRunnableRun
protected void shutdownRunnableRun()This method is therun()method of the shutdown runnable. It shuts down this executor. Subclasses may override it for additional shutdown behaviour. -
shutdownNow
This shutdown will reject new work (normal or flushing) and abort the current work: the pending work is cancelled, the work queue of the pool is drained and the threads executing work are interrupted. Since a flush only requests the termination of the running work, interrupting is the only means to end work which does not terminate by itself.In contrast to
shutdown()this may be called repeatedly and after a graceful shutdown, which it escalates. Each call returns the work it has aborted itself, that is, the outstanding work of the objects it has aborted as well as the work of callers which it has drained from the queue of the pool and cancelled. The internal work of this executor is not returned, a caller cannot use it.- Specified by:
shutdownNowin interfaceExecutorService- Overrides:
shutdownNowin classThreadPoolExecutor
-
getOutstandingRunnables
Gets a list of runnables which have not been executed based on the designated outstanding work.
This method will be called after an immediate shutdown which aborts pending work. Depending on the implementation, the designated runtime data may contain runnables which have not been executed and should therefore be returned to conform to the interface of an executor service.
The caller does not have the lock onobjects.- Parameters:
outstandingWork- The pending work that has been aborted due to the immediate shutdown.- Returns:
- A list of runnables stemming from the designated outstanding work.
-
isShutdown
public boolean isShutdown()Returns whether this executor has been shut down, independently of whether there is still work pending. UseThreadPoolExecutor.isTerminated()to find out whether the work has terminated as well.This is the shutdown status of this executor, not the one of the pool: the pool is shut down by a flush and therefore only after the work awaited by that flush has terminated. Where the state of the pool itself is required,
super.isShutdown()provides it.- Specified by:
isShutdownin interfaceExecutorService- Overrides:
isShutdownin classThreadPoolExecutor- Returns:
- Whether a shutdown of this executor has been requested.
-
awaitTermination
This also awaits the termination of the dedicated threads executing flush tasks which exceeded the limit of flush tasks within the pool.- Specified by:
awaitTerminationin interfaceExecutorService- Overrides:
awaitTerminationin classThreadPoolExecutor- Throws:
InterruptedException
-
AbstractObjectSpecificExecutor(int, int, Adept2ThreadFactory)instead.