Package org.jgroups.util
Class HashedTimingWheel
java.lang.Object
org.jgroups.util.HashedTimingWheel
- All Implemented Interfaces:
Runnable,ThreadManager,TimeScheduler
Implementation of
TimeScheduler. Uses a hashed timing wheel [1].
[1] http://www.cse.wustl.edu/~cdgill/courses/cs6874/TimingWheels.ppt- Author:
- Bela Ban
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classSimple task wrapper, always executed by at most 1 thread.Nested classes/interfaces inherited from interface org.jgroups.util.TimeScheduler
TimeScheduler.Task -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Logprotected final longprotected booleanprotected ThreadDecoratorprotected longprotected ThreadFactoryprotected final List<HashedTimingWheel.MyTask>[]protected intprotected int -
Constructor Summary
ConstructorsConstructorDescriptionCreate a scheduler that executes tasks in dynamically adjustable intervalsHashedTimingWheel(int corePoolSize) HashedTimingWheel(ThreadFactory factory, int min_threads, int max_threads, long keep_alive_time, int max_queue_size, int wheel_size, long tick_time) -
Method Summary
Modifier and TypeMethodDescriptionprotected void_run()Returns a list of tasks currently waiting for execution.voidExecutes command with zero required delay.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 applicableintGets the ThreadDecorator associated with this manager.protected voidinit()booleanReturns true if stop() has been called, false otherwisevoidrun()Future<?> Creates and executes a one-shot action that becomes enabled after the given delay.Future<?> scheduleAtFixedRate(Runnable task, long initial_delay, long delay, TimeUnit unit) Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.Future<?> Schedule a task for execution at varying intervals.Future<?> scheduleWithFixedDelay(Runnable task, long initial_delay, 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()Returns the number of tasks currently in the timerprotected voidvoidstop()Stops the timer, cancelling all tasksprotected voidtoString()
-
Field Details
-
running
protected volatile boolean running -
log
-
threadDecorator
-
timer_thread_factory
-
wheel_size
protected int wheel_size -
tick_time
protected long tick_time -
ROTATION_TIME
protected final long ROTATION_TIME -
wheel
-
wheel_position
protected int wheel_position
-
-
Constructor Details
-
HashedTimingWheel
public HashedTimingWheel()Create a scheduler that executes tasks in dynamically adjustable intervals -
HashedTimingWheel
public HashedTimingWheel(ThreadFactory factory, int min_threads, int max_threads, long keep_alive_time, int max_queue_size, int wheel_size, long tick_time) -
HashedTimingWheel
public HashedTimingWheel(int corePoolSize)
-
-
Method Details
-
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.
-
setThreadFactory
- Specified by:
setThreadFactoryin interfaceTimeScheduler
-
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
-
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:
-
getQueueSize
public int getQueueSize() -
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:
-
execute
Description copied from interface:TimeSchedulerExecutes command with zero required delay. This has effect equivalent to schedule(command, 0, anyUnit).- Specified by:
executein interfaceTimeScheduler- Parameters:
task- the task to execute
-
schedule
Description copied from interface:TimeSchedulerCreates and executes a one-shot action that becomes enabled after the given delay.- Specified by:
schedulein interfaceTimeScheduler- Parameters:
work- the task to executedelay- the time from now to delay executionunit- the time unit of the delay parameter- Returns:
- a ScheduledFuture representing pending completion of the task and whose get() method will return null upon completion
-
scheduleWithFixedDelay
public Future<?> scheduleWithFixedDelay(Runnable task, long initial_delay, 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 interfaceTimeScheduler- Parameters:
task- the task to executeinitial_delay- 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
-
scheduleAtFixedRate
Description copied from interface:TimeSchedulerCreates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. 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. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.- Specified by:
scheduleAtFixedRatein interfaceTimeScheduler- Parameters:
task- the task to executeinitial_delay- the time to delay first executiondelay- the period between successive executionsunit- the time unit of the initialDelay and period parameters- Returns:
- a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
-
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 execute 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*
-
size
public int size()Returns the number of tasks currently in the timer- Specified by:
sizein interfaceTimeScheduler- Returns:
- The number of tasks currently in the timer
-
toString
-
stop
public void stop()Stops the timer, cancelling all tasks- Specified by:
stopin interfaceTimeScheduler- Throws:
InterruptedException- if interrupted while waiting for thread to return
-
isShutdown
public boolean isShutdown()Description copied from interface:TimeSchedulerReturns true if stop() has been called, false otherwise- Specified by:
isShutdownin interfaceTimeScheduler
-
run
public void run() -
_run
protected void _run() -
init
protected void init() -
startRunner
protected void startRunner() -
stopRunner
protected void stopRunner()
-