Package org.jgroups.util
Class DefaultTimeScheduler
java.lang.Object
java.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ScheduledThreadPoolExecutor
org.jgroups.util.DefaultTimeScheduler
- All Implemented Interfaces:
AutoCloseable,Executor,ExecutorService,ScheduledExecutorService,ThreadManager,TimeScheduler
Implementation of
TimeScheduler by extending
ScheduledThreadPoolExecutor to keep tasks sorted. Tasks will get executed in order
of execution time (by using a DelayQueue internally.- Author:
- Bela Ban
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicyNested classes/interfaces inherited from interface org.jgroups.util.TimeScheduler
TimeScheduler.Task -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreate a scheduler that executes tasks in dynamically adjustable intervalsDefaultTimeScheduler(int corePoolSize) DefaultTimeScheduler(ThreadFactory factory) DefaultTimeScheduler(ThreadFactory factory, int max_threads) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterExecute(Runnable r, Throwable t) Returns a list of tasks currently waiting for execution.intReturns the current threads in the pool, or -1 if not applicablelongReturns the keep alive time (in ms) of the thread pool, or -1 if not applicableintReturns the configured max threads, or -1 if not applicableintReturns the configured core threads, or -1 if not applicableGets the ThreadDecorator associated with this manager.Future<?> Schedule a task for execution at varying intervals.scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.voidsetKeepAliveTime(long time) Sets the keep alive time (in ms) of the thread pool.voidsetMaxThreads(int size) Sets the max pool size.voidsetMinThreads(int size) Sets the core pool size.voidsetThreadDecorator(ThreadDecorator threadDecorator) Sets the ThreadDecorator associated this manager should use.voidsetThreadFactory(ThreadFactory factory) intsize()Answers the number of tasks currently in the queue.voidstop()Stop the scheduler if it's running.toString()Methods inherited from class java.util.concurrent.ScheduledThreadPoolExecutor
decorateTask, decorateTask, execute, getContinueExistingPeriodicTasksAfterShutdownPolicy, getExecuteExistingDelayedTasksAfterShutdownPolicy, getQueue, getRemoveOnCancelPolicy, schedule, schedule, scheduleAtFixedRate, setContinueExistingPeriodicTasksAfterShutdownPolicy, setExecuteExistingDelayedTasksAfterShutdownPolicy, setRemoveOnCancelPolicy, shutdown, shutdownNow, submit, submit, submitMethods inherited from class java.util.concurrent.ThreadPoolExecutor
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, terminatedMethods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskForMethods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, close, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminatedMethods inherited from interface org.jgroups.util.TimeScheduler
execute, isShutdown, schedule, scheduleAtFixedRate
-
Field Details
-
log
-
-
Constructor Details
-
DefaultTimeScheduler
public DefaultTimeScheduler()Create a scheduler that executes tasks in dynamically adjustable intervals -
DefaultTimeScheduler
-
DefaultTimeScheduler
-
DefaultTimeScheduler
public DefaultTimeScheduler(int corePoolSize)
-
-
Method Details
-
setThreadFactory
- Specified by:
setThreadFactoryin interfaceTimeScheduler
-
getThreadDecorator
Description copied from interface:ThreadManagerGets the ThreadDecorator associated with this manager.- Specified by:
getThreadDecoratorin interfaceThreadManager- Returns:
- the ThreadDecorator, or
nullif there is none.
-
setThreadDecorator
Description copied from interface:ThreadManagerSets the ThreadDecorator associated this manager should use.- Specified by:
setThreadDecoratorin interfaceThreadManager- Parameters:
threadDecorator- the ThreadDecorator, ornull.
-
dumpTimerTasks
Description copied from interface:TimeSchedulerReturns a list of tasks currently waiting for execution. If there are a lot of tasks, the returned string should probably only return the number of tasks rather than a full dump.- Specified by:
dumpTimerTasksin interfaceTimeScheduler- Returns:
-
getCurrentThreads
public int getCurrentThreads()Description copied from interface:TimeSchedulerReturns the current threads in the pool, or -1 if not applicable- Specified by:
getCurrentThreadsin interfaceTimeScheduler- Returns:
-
getMinThreads
public int getMinThreads()Description copied from interface:TimeSchedulerReturns the configured core threads, or -1 if not applicable- Specified by:
getMinThreadsin interfaceTimeScheduler- Returns:
-
setMinThreads
public void setMinThreads(int size) Description copied from interface:TimeSchedulerSets the core pool size. Can be ignored if not applicable- Specified by:
setMinThreadsin interfaceTimeScheduler
-
getMaxThreads
public int getMaxThreads()Description copied from interface:TimeSchedulerReturns the configured max threads, or -1 if not applicable- Specified by:
getMaxThreadsin interfaceTimeScheduler- Returns:
-
setMaxThreads
public void setMaxThreads(int size) Description copied from interface:TimeSchedulerSets the max pool size. Can be ignored if not applicable- Specified by:
setMaxThreadsin interfaceTimeScheduler
-
getKeepAliveTime
public long getKeepAliveTime()Description copied from interface:TimeSchedulerReturns the keep alive time (in ms) of the thread pool, or -1 if not applicable- Specified by:
getKeepAliveTimein interfaceTimeScheduler
-
setKeepAliveTime
public void setKeepAliveTime(long time) Description copied from interface:TimeSchedulerSets the keep alive time (in ms) of the thread pool. Can be ignored if not applicable- Specified by:
setKeepAliveTimein interfaceTimeScheduler
-
scheduleWithDynamicInterval
Schedule a task for execution at varying intervals. After execution, the task will get rescheduled afterTimeScheduler.Task.nextInterval()milliseconds. The task is neve done until nextInterval() return a value invalid input: '<'= 0 or the task is cancelled.- Specified by:
scheduleWithDynamicIntervalin interfaceTimeScheduler- Parameters:
task- the task to executerelative- scheduling scheme: true:
Task is rescheduled relative to the last time it actually started execution false:
Task is scheduled relative to its last execution schedule. This has the effect that the time between two consecutive executions of the task remains the same. Note that relative is always true; we always schedule the next execution relative to the last *actual* (not scheduled) execution
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) Description copied from interface:TimeSchedulerCreates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.- Specified by:
scheduleWithFixedDelayin interfaceScheduledExecutorService- Specified by:
scheduleWithFixedDelayin interfaceTimeScheduler- Overrides:
scheduleWithFixedDelayin classScheduledThreadPoolExecutor- Parameters:
command- the task to executeinitialDelay- the time to delay first executiondelay- the delay between the termination of one execution and the commencement of the nextunit- the time unit of the initialDelay and delay parameters- Returns:
- a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
-
size
public int size()Answers the number of tasks currently in the queue.- Specified by:
sizein interfaceTimeScheduler- Returns:
- The number of tasks currently in the queue.
-
stop
public void stop()Stop the scheduler if it's running. Switch to stopped, if it's suspended. Clear the task queue, cancelling all un-executed tasks- Specified by:
stopin interfaceTimeScheduler- Throws:
InterruptedException- if interrupted while waiting for thread to return
-
afterExecute
- Overrides:
afterExecutein classThreadPoolExecutor
-
toString
- Overrides:
toStringin classThreadPoolExecutor
-