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 aDelayQueue
to order tasks according to execution times- Since:
- 3.3
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
TimeScheduler3.RecurringTask
Tasks which runs more than once, either dynamic, fixed-rate or fixed-delay, until cancelledstatic class
TimeScheduler3.Task
protected static class
TimeScheduler3.TaskType
-
Field Summary
Fields Modifier and Type Field Description protected static Log
log
protected boolean
non_blocking_task_handling
protected java.util.concurrent.Executor
pool
Thread pool used to execute the tasksprotected java.util.concurrent.BlockingQueue<TimeScheduler3.Task>
queue
DelayQueue with tasks being sorted according to execution times (next execution first)protected java.lang.Thread
runner
Thread which removes tasks ready to be executed from the queue and submits them to the pool for executionprotected boolean
shut_down_pool
protected ThreadFactory
timer_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.Task
add(TimeScheduler3.Task task)
protected <T> T
condGet(java.util.function.Function<java.util.concurrent.ThreadPoolExecutor,T> getter, T default_value)
protected void
condSet(java.util.function.Consumer<java.util.concurrent.ThreadPoolExecutor> setter)
protected java.util.concurrent.Future<?>
doSchedule(TimeScheduler3.Task task, long initial_delay)
java.lang.String
dumpTimerTasks()
Returns a list of tasks currently waiting for execution.void
execute(java.lang.Runnable task, boolean can_block)
int
getCurrentThreads()
Returns the current threads in the pool, or -1 if not applicablelong
getKeepAliveTime()
Returns the keep alive time (in ms) of the thread pool, or -1 if not applicableint
getMaxThreads()
Returns the configured max threads, or -1 if not applicableint
getMinThreads()
Returns the configured core threads, or -1 if not applicableboolean
getNonBlockingTaskHandling()
int
getQueueSize()
protected boolean
isRunning()
boolean
isShutdown()
Returns true if stop() has been called, false otherwisevoid
removeCancelledTasks()
void
run()
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)
void
setKeepAliveTime(long time)
Sets the keep alive time (in ms) of the thread pool.void
setMaxThreads(int size)
Sets the max pool size.void
setMinThreads(int size)
Sets the core pool size.void
setNonBlockingTaskHandling(boolean b)
void
setThreadFactory(ThreadFactory f)
void
setThreadPool(java.util.concurrent.Executor new_pool)
int
size()
Returns the number of tasks currently in the queue.void
start()
Starts the runner threadvoid
stop()
Stops the timer, cancelling all tasksprotected void
submitToPool(TimeScheduler3.Task task)
java.lang.String
toString()
-
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:
setThreadFactory
in interfaceTimeScheduler
-
setThreadPool
public void setThreadPool(java.util.concurrent.Executor new_pool)
-
getMinThreads
public int getMinThreads()
Description copied from interface:TimeScheduler
Returns the configured core threads, or -1 if not applicable- Specified by:
getMinThreads
in interfaceTimeScheduler
- Returns:
-
setMinThreads
public void setMinThreads(int size)
Description copied from interface:TimeScheduler
Sets the core pool size. Can be ignored if not applicable- Specified by:
setMinThreads
in interfaceTimeScheduler
-
getMaxThreads
public int getMaxThreads()
Description copied from interface:TimeScheduler
Returns the configured max threads, or -1 if not applicable- Specified by:
getMaxThreads
in interfaceTimeScheduler
- Returns:
-
setMaxThreads
public void setMaxThreads(int size)
Description copied from interface:TimeScheduler
Sets the max pool size. Can be ignored if not applicable- Specified by:
setMaxThreads
in interfaceTimeScheduler
-
getKeepAliveTime
public long getKeepAliveTime()
Description copied from interface:TimeScheduler
Returns the keep alive time (in ms) of the thread pool, or -1 if not applicable- Specified by:
getKeepAliveTime
in interfaceTimeScheduler
-
setKeepAliveTime
public void setKeepAliveTime(long time)
Description copied from interface:TimeScheduler
Sets the keep alive time (in ms) of the thread pool. Can be ignored if not applicable- Specified by:
setKeepAliveTime
in interfaceTimeScheduler
-
getCurrentThreads
public int getCurrentThreads()
Description copied from interface:TimeScheduler
Returns the current threads in the pool, or -1 if not applicable- Specified by:
getCurrentThreads
in interfaceTimeScheduler
- Returns:
-
getQueueSize
public int getQueueSize()
-
size
public int size()
Description copied from interface:TimeScheduler
Returns the number of tasks currently in the queue.- Specified by:
size
in interfaceTimeScheduler
- Returns:
- The number of tasks currently in the queue.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
isShutdown
public boolean isShutdown()
Description copied from interface:TimeScheduler
Returns true if stop() has been called, false otherwise- Specified by:
isShutdown
in interfaceTimeScheduler
-
getNonBlockingTaskHandling
public boolean getNonBlockingTaskHandling()
- Specified by:
getNonBlockingTaskHandling
in interfaceTimeScheduler
-
setNonBlockingTaskHandling
public void setNonBlockingTaskHandling(boolean b)
- Specified by:
setNonBlockingTaskHandling
in interfaceTimeScheduler
-
dumpTimerTasks
public java.lang.String dumpTimerTasks()
Description copied from interface:TimeScheduler
Returns 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:
dumpTimerTasks
in interfaceTimeScheduler
- Returns:
-
removeCancelledTasks
public void removeCancelledTasks()
- Specified by:
removeCancelledTasks
in interfaceTimeScheduler
-
execute
public void execute(java.lang.Runnable task, boolean can_block)
- Specified by:
execute
in 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:
schedule
in 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:
scheduleWithFixedDelay
in 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:
scheduleAtFixedRate
in 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:
scheduleWithDynamicInterval
in interfaceTimeScheduler
- Parameters:
work
- the task to execute
-
start
public void start()
Description copied from interface:TimeScheduler
Starts the runner thread- Specified by:
start
in interfaceTimeScheduler
-
stop
public void stop()
Stops the timer, cancelling all tasks- Specified by:
stop
in interfaceTimeScheduler
-
run
public void run()
- Specified by:
run
in 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()
-
-