public class LoggingScheduledExecutor extends ScheduledThreadPoolExecutor
ThreadPoolExecutor.afterExecute(java.lang.Runnable, java.lang.Throwable)
and ScheduledThreadPoolExecutor.decorateTask(java.lang.Runnable, java.util.concurrent.RunnableScheduledFuture<V>)
methods.
Reasoning is given tasks to ScheduledThreadPoolExecutor
stops silently if there is an
execution exception.
Note: Task decoration is only needed to call given tasks toString
methods.
ScheduledExecutorService.scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
Remove on cancel:
To prevent running into an accumulation of cancelled task which can cause gc related problems
(e.g. transactions in combination with LockResource eviction), it is best that tasks are
removed from the scheduler on cancellation.
In Java 7+ the there is a method ScheduledThreadPoolExecutor#setRemoveOnCancelPolicy(boolean)
which removes the runnable on cancellation. Removal of tasks is done in logarithmic time
(see ScheduledThreadPoolExecutor.DelayedWorkQueue.indexOf where there is a direct lookup
instead of a linear scan over the queue). So in Java 7 we try to set this method using
reflection. In Java 7+ the manualRemoveOnCancel is ignored, since it has an efficient removal
of cancelled tasks and therefore it will always apply it. If we would wrap the task with the
RemoveOnCancelFuture, it would even prevent constant time removal.
In Java 6 there is no such method setRemoveOnCancelPolicy and therefore the task is
wrapped in a RemoveOnCancelFuture which will remove itself from the work-queue
on cancellation. However this removal isn't done in constant time, but in linear time
because Java 6 ScheduledThreadPoolExecutor doesn't support a constant time removal.
By default in Java 6, the removal of this task is done based on the 'removeOnCancel'
property which gets its value from the
GroupProperty.TASK_SCHEDULER_REMOVE_ON_CANCEL
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
Constructor and Description |
---|
LoggingScheduledExecutor(ILogger logger,
int corePoolSize,
ThreadFactory threadFactory) |
LoggingScheduledExecutor(ILogger logger,
int corePoolSize,
ThreadFactory threadFactory,
boolean removeOnCancel) |
LoggingScheduledExecutor(ILogger logger,
int corePoolSize,
ThreadFactory threadFactory,
boolean removeOnCancel,
RejectedExecutionHandler handler) |
LoggingScheduledExecutor(ILogger logger,
int corePoolSize,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) |
Modifier and Type | Method and Description |
---|---|
protected void |
afterExecute(Runnable runnable,
Throwable throwable) |
protected <V> RunnableScheduledFuture<V> |
decorateTask(Callable<V> callable,
RunnableScheduledFuture<V> task) |
protected <V> RunnableScheduledFuture<V> |
decorateTask(Runnable runnable,
RunnableScheduledFuture<V> task) |
void |
notifyShutdownInitiated() |
execute, getContinueExistingPeriodicTasksAfterShutdownPolicy, getExecuteExistingDelayedTasksAfterShutdownPolicy, getQueue, getRemoveOnCancelPolicy, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, setContinueExistingPeriodicTasksAfterShutdownPolicy, setExecuteExistingDelayedTasksAfterShutdownPolicy, setRemoveOnCancelPolicy, shutdown, shutdownNow, submit, submit, submit
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, terminated, toString
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated
public LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory threadFactory)
public LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory threadFactory, boolean removeOnCancel)
public LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)
public LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory threadFactory, boolean removeOnCancel, RejectedExecutionHandler handler)
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)
decorateTask
in class ScheduledThreadPoolExecutor
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
decorateTask
in class ScheduledThreadPoolExecutor
protected void afterExecute(Runnable runnable, Throwable throwable)
afterExecute
in class ThreadPoolExecutor
public void notifyShutdownInitiated()
Copyright © 2018 Hazelcast, Inc.. All rights reserved.