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.public abstract class AbstractObjectSpecificExecutor<O1,O2,R> extends CachedThreadPoolExecutor
ThreadPoolExecutor. java.util.concurrent.ThreadPoolExecutor.AbortPolicy, java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardPolicy| Modifier and Type | Field and Description |
|---|---|
protected java.util.concurrent.atomic.AtomicInteger |
abortCount
A counter for all flushs that aborted pending work.
|
protected java.util.concurrent.atomic.AtomicInteger |
flushPending
A counter for pending flushs.
|
protected java.util.logging.Logger |
logger
The logger for log messages, especially runtime exceptions occurring when executing work.
|
protected java.util.Map<O2,Pair<ObjectRunnable<O1,O2>,R>> |
objects
All managed objects, their runnables as well as their runtime data.
|
protected java.util.concurrent.atomic.AtomicBoolean |
shutdownPending
Whether a shutdown has been requested.
|
fullSize, realCoreSize, realMaxSize, workSizeactiveThreads, completedTasks, submittedTasks| Modifier | Constructor and Description |
|---|---|
protected |
AbstractObjectSpecificExecutor(Adept2ThreadFactory threadFactory)
Creates a new thread pool executor with an unlimited amount of threads for different objects
using the designate thread factory for creating threads.
|
protected |
AbstractObjectSpecificExecutor(int limit,
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.
|
protected |
AbstractObjectSpecificExecutor(int corePoolSize,
int maxPoolSize,
boolean prestart,
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.
|
| Modifier and Type | Method and Description |
|---|---|
protected abstract ObjectRunnable<O1,O2> |
addRunnable(O1 registeredObject,
O2 indexObject,
java.lang.Runnable task)
Adds the designated runnable for the designated object and index object to this executor.
|
protected abstract <T> Pair<java.util.concurrent.Future<T>,java.util.Map<O2,R>> |
cancelAndFlush(boolean shutdownRequest,
java.util.Map<O2,R> pending,
java.util.concurrent.Callable<T> task,
java.util.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.
|
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.
|
void |
execute(O1 object,
java.lang.Runnable task)
Executes the designated task for the designated object.
|
protected abstract void |
executeShutdown(java.lang.Runnable shutdown)
Executes the designated shutdown task which contains a flush calling
ThreadPoolExecutor.shutdown() without abortion. |
<T> Pair<java.util.concurrent.Future<T>,java.util.Map<O2,R>> |
flushAndExecute(boolean abortPending,
java.util.concurrent.Callable<T> task)
Flushes all work and executes the designated task.
|
protected <T> Pair<java.util.concurrent.Future<T>,java.util.Map<O2,R>> |
flushAndExecuteUnchecked(boolean abortPending,
boolean shutdownRequest,
java.util.concurrent.Callable<T> task)
The same as
flushAndExecute(boolean, Callable) but without the check for a pending
shutdown. |
protected abstract O2 |
getIndexObjectFor(O1 object)
Gets the object used for managing and executing runnables within this executor.
|
protected abstract java.util.List<java.lang.Runnable> |
getOutstandingRunnables(java.util.Map<O2,R> outstandingWork)
Gets a list of runnables which have not been executed based on the designated outstanding work.
|
boolean |
isShutdown() |
protected boolean |
restart(boolean abortedPending)
Restarts the work after a flush.
|
protected void |
restartPendingWork()
Actually restarts by inspecting all existing objects for whether there is still a runnable for
it.
|
void |
shutdown()
Shutting down will reject new work (normal or flushing).
|
java.util.List<java.lang.Runnable> |
shutdownNow()
This shutdown will reject new work (normal or flushing) and abort the current work except of
pending flushs.
|
protected void |
shutdownRunnableRun()
This method is the
run() method of the shutdown runnable. |
<T> java.util.concurrent.Future<T> |
submit(O1 object,
java.util.concurrent.Callable<T> task)
Submits the designated task for the designated object.
|
java.util.concurrent.Future<?> |
submit(O1 object,
java.lang.Runnable task)
Submits the designated task for the designated object.
|
<T> Pair<java.util.concurrent.Future<T>,java.util.Map<O2,R>> |
submitAndFlush(boolean abortPending,
java.util.concurrent.Callable<T> task)
Submits the designated task and flushes all work.
|
afterExecute, executebeforeExecute, newTaskFor, newTaskFor, setLogExecuteCallStack, updateQueueLimitallowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, terminated, toStringprotected final java.util.Map<O2,Pair<ObjectRunnable<O1,O2>,R>> objects
protected final java.util.concurrent.atomic.AtomicInteger flushPending
objects.protected final java.util.concurrent.atomic.AtomicInteger abortCount
objects.protected final java.util.concurrent.atomic.AtomicBoolean shutdownPending
protected final java.util.logging.Logger logger
protected AbstractObjectSpecificExecutor(Adept2ThreadFactory threadFactory)
threadFactory - The factory to use when the executor creates a new thread.protected AbstractObjectSpecificExecutor(int limit,
Adept2ThreadFactory threadFactory)
limit - The maximum number of threads allowed in the pool.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.protected AbstractObjectSpecificExecutor(int corePoolSize,
int maxPoolSize,
boolean prestart,
Adept2ThreadFactory threadFactory)
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.public void execute(O1 object, java.lang.Runnable task)
object - The object for which to execute the designated task.task - The task which to execute.java.util.concurrent.RejectedExecutionException - if the executor has been shut
down, a RejectedExecutionException will be thrown.CachedThreadPoolExecutor.execute(Runnable)protected abstract O2 getIndexObjectFor(O1 object)
object - The object for which a runnable is registered.protected abstract ObjectRunnable<O1,O2> addRunnable(O1 registeredObject, O2 indexObject, java.lang.Runnable task)
CachedThreadPoolExecutor.execute(Runnable). If they return null, they
have to assign the runnable to an active element eventually and arbitrarily.
The caller has the lock on objects.
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.CachedThreadPoolExecutor.execute(Runnable), which means the runnable
will be executed independently from other work and a new thread may be started for it.public <T> Pair<java.util.concurrent.Future<T>,java.util.Map<O2,R>> flushAndExecute(boolean abortPending, java.util.concurrent.Callable<T> task)
null will be returned and the object work will
restart after the designated task has been executed. 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.null in case no abortion is requested otherwise the managed objects
and the runtime data of the aborted work.java.util.concurrent.RejectedExecutionException - if the executor has been shut
down, a RejectedExecutionException will be thrown.protected <T> Pair<java.util.concurrent.Future<T>,java.util.Map<O2,R>> flushAndExecuteUnchecked(boolean abortPending, boolean shutdownRequest, java.util.concurrent.Callable<T> task)
flushAndExecute(boolean, Callable) but without the check for a pending
shutdown. This is required for the shutdown procedure.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 a shutdownNow(). 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.null in case no abortion is requested otherwise the managed objects
and the runtime data of the aborted work.java.util.concurrent.RejectedExecutionException - if the executor has been shut
down, a RejectedExecutionException will be thrown.protected abstract void clearAbortedPendingWork(boolean shutdownRequest)
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.
shutdownRequest - Whether this flush is caused by a shutdownNow(). In this case
all work has to be aborted, even the work pending after a flush.protected abstract <T> Pair<java.util.concurrent.Future<T>,java.util.Map<O2,R>> cancelAndFlush(boolean shutdownRequest, java.util.Map<O2,R> pending, java.util.concurrent.Callable<T> task, java.util.Collection<ObjectRunnable<O1,O2>> objectRunnables)
The caller has the lock on objects.
shutdownRequest - Whether this flush is caused by a shutdownNow(). 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 or null if
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.null in 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.public java.util.concurrent.Future<?> submit(O1 object, java.lang.Runnable task)
object - The object for which to execute the designated task.task - The task which to execute.Future representing pending completion of the taskjava.util.concurrent.RejectedExecutionException - if the executor has been shut
down, a RejectedExecutionException will be thrown.AbstractExecutorService.submit(Runnable)public <T> java.util.concurrent.Future<T> submit(O1 object, java.util.concurrent.Callable<T> task)
object - The object for which to execute the designated task.task - The task which to execute.Future representing pending completion of the taskjava.util.concurrent.RejectedExecutionException - if the executor has been shut
down, a RejectedExecutionException will be thrown.AbstractExecutorService.submit(Callable)public <T> Pair<java.util.concurrent.Future<T>,java.util.Map<O2,R>> submitAndFlush(boolean abortPending, java.util.concurrent.Callable<T> task)
null will be returned and the work will restart
after the designated task has been executed. 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.null in case no abortion is requested otherwise the managed objects
and the runtime data of the aborted work.java.util.concurrent.RejectedExecutionException - if the executor has been shut
down.flushAndExecute(boolean, Callable)protected boolean restart(boolean abortedPending)
abortedPending - Whether the flush leading to this restart has aborted pending work.protected void restartPendingWork()
The caller has the lock on objects.
public void shutdown()
shutdown in interface java.util.concurrent.ExecutorServiceshutdown in class java.util.concurrent.ThreadPoolExecutorprotected abstract void executeShutdown(java.lang.Runnable shutdown)
ThreadPoolExecutor.shutdown() without abortion.
The caller has the lock on objects.
shutdown - The task which to perform to shutdown.protected void shutdownRunnableRun()
run() method of the shutdown runnable. It shuts down this
executor. Subclasses may override it for additional shutdown behaviour.public java.util.List<java.lang.Runnable> shutdownNow()
shutdownNow in interface java.util.concurrent.ExecutorServiceshutdownNow in class java.util.concurrent.ThreadPoolExecutorprotected abstract java.util.List<java.lang.Runnable> getOutstandingRunnables(java.util.Map<O2,R> outstandingWork)
objects.outstandingWork - The pending work that has been aborted due to the immediate shutdown.public boolean isShutdown()
isShutdown in interface java.util.concurrent.ExecutorServiceisShutdown in class java.util.concurrent.ThreadPoolExecutor