Package org.jgroups.util
Class TimeScheduler3
- java.lang.Object
-
- org.jgroups.util.TimeScheduler3
-
- All Implemented Interfaces:
java.lang.Runnable,TimeScheduler
public class TimeScheduler3 extends java.lang.Object implements TimeScheduler, java.lang.Runnable
Implementation ofTimeScheduler. Uses aDelayQueueto order tasks according to execution times- Since:
- 3.3
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classTimeScheduler3.RecurringTaskTasks which runs more than once, either dynamic, fixed-rate or fixed-delay, until cancelledstatic classTimeScheduler3.Taskprotected static classTimeScheduler3.TaskType
-
Field Summary
Fields Modifier and Type Field Description protected static Loglogprotected booleannon_blocking_task_handlingprotected java.util.concurrent.ExecutorpoolThread pool used to execute the tasksprotected java.util.concurrent.BlockingQueue<TimeScheduler3.Task>queueDelayQueue with tasks being sorted according to execution times (next execution first)protected java.lang.ThreadrunnerThread which removes tasks ready to be executed from the queue and submits them to the pool for executionprotected booleanshut_down_poolprotected ThreadFactorytimer_thread_factory
-
Constructor Summary
Constructors Constructor Description TimeScheduler3()Create a scheduler that executes tasks in dynamically adjustable intervalsTimeScheduler3(java.util.concurrent.Executor thread_pool, ThreadFactory factory, boolean start)TimeScheduler3(ThreadFactory factory, int min_threads, int max_threads, long keep_alive_time, int max_queue_size, java.lang.String rejection_policy)TimeScheduler3(ThreadFactory factory, int min_threads, int max_threads, long keep_alive_time, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, java.lang.String rejection_policy, boolean thread_pool_enabled)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected TimeScheduler3.Taskadd(TimeScheduler3.Task task)protected <T> TcondGet(java.util.function.Function<java.util.concurrent.ThreadPoolExecutor,T> getter, T default_value)protected voidcondSet(java.util.function.Consumer<java.util.concurrent.ThreadPoolExecutor> setter)protected java.util.concurrent.Future<?>doSchedule(TimeScheduler3.Task task, long initial_delay)java.lang.StringdumpTimerTasks()Returns a list of tasks currently waiting for execution.voidexecute(java.lang.Runnable task, boolean can_block)intgetCurrentThreads()Returns the current threads in the pool, or -1 if not applicablelonggetKeepAliveTime()Returns the keep alive time (in ms) of the thread pool, or -1 if not applicableintgetMaxThreads()Returns the configured max threads, or -1 if not applicableintgetMinThreads()Returns the configured core threads, or -1 if not applicablebooleangetNonBlockingTaskHandling()intgetQueueSize()protected booleanisRunning()booleanisShutdown()Returns true if stop() has been called, false otherwisevoidremoveCancelledTasks()voidrun()java.util.concurrent.Future<?>schedule(java.lang.Runnable work, long initial_delay, java.util.concurrent.TimeUnit unit, boolean can_block)java.util.concurrent.Future<?>scheduleAtFixedRate(java.lang.Runnable work, long initial_delay, long delay, java.util.concurrent.TimeUnit unit, boolean can_block)protected java.util.concurrent.Future<?>scheduleRecurring(java.lang.Runnable work, TimeScheduler3.TaskType type, long initial_delay, long delay, java.util.concurrent.TimeUnit unit, boolean can_block)java.util.concurrent.Future<?>scheduleWithDynamicInterval(TimeScheduler.Task work, boolean can_block)Schedule a task for execution at varying intervals.java.util.concurrent.Future<?>scheduleWithFixedDelay(java.lang.Runnable work, long initial_delay, long delay, java.util.concurrent.TimeUnit unit, boolean can_block)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.voidsetNonBlockingTaskHandling(boolean b)voidsetThreadFactory(ThreadFactory f)voidsetThreadPool(java.util.concurrent.Executor new_pool)intsize()Returns the number of tasks currently in the queue.voidstart()Starts the runner threadvoidstop()Stops the timer, cancelling all tasksprotected voidsubmitToPool(TimeScheduler3.Task task)java.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.jgroups.util.TimeScheduler
execute, schedule, scheduleAtFixedRate, scheduleWithDynamicInterval, scheduleWithFixedDelay
-
-
-
-
Field Detail
-
pool
protected java.util.concurrent.Executor pool
Thread pool used to execute the tasks
-
queue
protected final java.util.concurrent.BlockingQueue<TimeScheduler3.Task> queue
DelayQueue with tasks being sorted according to execution times (next execution first)
-
runner
protected volatile java.lang.Thread runner
Thread which removes tasks ready to be executed from the queue and submits them to the pool for execution
-
log
protected static final Log log
-
timer_thread_factory
protected ThreadFactory timer_thread_factory
-
non_blocking_task_handling
protected boolean non_blocking_task_handling
-
shut_down_pool
protected boolean shut_down_pool
-
-
Constructor Detail
-
TimeScheduler3
public TimeScheduler3()
Create a scheduler that executes tasks in dynamically adjustable intervals
-
TimeScheduler3
public TimeScheduler3(ThreadFactory factory, int min_threads, int max_threads, long keep_alive_time, int max_queue_size, java.lang.String rejection_policy)
-
TimeScheduler3
public TimeScheduler3(ThreadFactory factory, int min_threads, int max_threads, long keep_alive_time, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, java.lang.String rejection_policy, boolean thread_pool_enabled)
-
TimeScheduler3
public TimeScheduler3(java.util.concurrent.Executor thread_pool, ThreadFactory factory, boolean start)
-
-
Method Detail
-
setThreadFactory
public void setThreadFactory(ThreadFactory f)
- Specified by:
setThreadFactoryin interfaceTimeScheduler
-
setThreadPool
public void setThreadPool(java.util.concurrent.Executor new_pool)
-
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()
-
size
public int size()
Description copied from interface:TimeSchedulerReturns the number of tasks currently in the queue.- Specified by:
sizein interfaceTimeScheduler- Returns:
- The number of tasks currently in the queue.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
isShutdown
public boolean isShutdown()
Description copied from interface:TimeSchedulerReturns true if stop() has been called, false otherwise- Specified by:
isShutdownin interfaceTimeScheduler
-
getNonBlockingTaskHandling
public boolean getNonBlockingTaskHandling()
- Specified by:
getNonBlockingTaskHandlingin interfaceTimeScheduler
-
setNonBlockingTaskHandling
public void setNonBlockingTaskHandling(boolean b)
- Specified by:
setNonBlockingTaskHandlingin interfaceTimeScheduler
-
dumpTimerTasks
public java.lang.String 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:
-
removeCancelledTasks
public void removeCancelledTasks()
- Specified by:
removeCancelledTasksin interfaceTimeScheduler
-
execute
public void execute(java.lang.Runnable task, boolean can_block)- Specified by:
executein interfaceTimeScheduler
-
schedule
public java.util.concurrent.Future<?> schedule(java.lang.Runnable work, long initial_delay, java.util.concurrent.TimeUnit unit, boolean can_block)- Specified by:
schedulein interfaceTimeScheduler
-
scheduleWithFixedDelay
public java.util.concurrent.Future<?> scheduleWithFixedDelay(java.lang.Runnable work, long initial_delay, long delay, java.util.concurrent.TimeUnit unit, boolean can_block)- Specified by:
scheduleWithFixedDelayin interfaceTimeScheduler
-
scheduleAtFixedRate
public java.util.concurrent.Future<?> scheduleAtFixedRate(java.lang.Runnable work, long initial_delay, long delay, java.util.concurrent.TimeUnit unit, boolean can_block)- Specified by:
scheduleAtFixedRatein interfaceTimeScheduler
-
scheduleWithDynamicInterval
public java.util.concurrent.Future<?> scheduleWithDynamicInterval(TimeScheduler.Task work, boolean can_block)
Schedule a task for execution at varying intervals. After execution, the task will get rescheduled afterTimeScheduler.Task.nextInterval()milliseconds. The task is never done until nextInterval() returns a value <= 0 or the task is cancelled. Note that the task is rescheduled relative to the last time is actually executed. This is similar toTimeScheduler.scheduleWithFixedDelay(Runnable,long,long,java.util.concurrent.TimeUnit).- Specified by:
scheduleWithDynamicIntervalin interfaceTimeScheduler- Parameters:
work- the task to execute
-
start
public void start()
Description copied from interface:TimeSchedulerStarts the runner thread- Specified by:
startin interfaceTimeScheduler
-
stop
public void stop()
Stops the timer, cancelling all tasks- Specified by:
stopin interfaceTimeScheduler
-
run
public void run()
- Specified by:
runin interfacejava.lang.Runnable
-
scheduleRecurring
protected java.util.concurrent.Future<?> scheduleRecurring(java.lang.Runnable work, TimeScheduler3.TaskType type, long initial_delay, long delay, java.util.concurrent.TimeUnit unit, boolean can_block)
-
doSchedule
protected java.util.concurrent.Future<?> doSchedule(TimeScheduler3.Task task, long initial_delay)
-
condSet
protected void condSet(java.util.function.Consumer<java.util.concurrent.ThreadPoolExecutor> setter)
-
condGet
protected <T> T condGet(java.util.function.Function<java.util.concurrent.ThreadPoolExecutor,T> getter, T default_value)
-
submitToPool
protected void submitToPool(TimeScheduler3.Task task)
-
add
protected TimeScheduler3.Task add(TimeScheduler3.Task task)
-
isRunning
protected boolean isRunning()
-
-