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:
  java.util.concurrent.Executor, java.util.concurrent.ExecutorService, java.util.concurrent.ScheduledExecutorService, ThreadManager, TimeScheduler

    public class DefaultTimeScheduler
    extends java.util.concurrent.ScheduledThreadPoolExecutor
    implements 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

`java.util.concurrent.ThreadPoolExecutor.AbortPolicy, java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardPolicy`

### Nested classes/interfaces inherited from interface org.jgroups.util.TimeScheduler

`TimeScheduler.Task`

### Field Summary

Fields
`protected static Log` `log`

### Constructor Summary

Constructors
Constructor and Description
`DefaultTimeScheduler()` Create a scheduler that executes tasks in dynamically adjustable intervals
`DefaultTimeScheduler(int corePoolSize)`
`DefaultTimeScheduler(ThreadFactory factory)`
`DefaultTimeScheduler(ThreadFactory factory, int max_threads)`

### Method Summary

Methods
`protected void` `afterExecute(java.lang.Runnable r, java.lang.Throwable t)`
`java.lang.String` `dumpTimerTasks()` Returns a list of tasks currently waiting for execution.
`int` `getCurrentThreads()` Returns the current threads in the pool, or -1 if not applicable
`long` `getKeepAliveTime()` Returns the keep alive time (in ms) of the thread pool, or -1 if not applicable
`int` `getMaxThreads()` Returns the configured max threads, or -1 if not applicable
`int` `getMinThreads()` Returns the configured core threads, or -1 if not applicable
`ThreadDecorator` `getThreadDecorator()` Gets the ThreadDecorator associated with this manager.
`java.util.concurrent.Future<?>` `scheduleWithDynamicInterval(TimeScheduler.Task task)` Schedule a task for execution at varying intervals.
`java.util.concurrent.ScheduledFuture<?>` `scheduleWithFixedDelay(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.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.
`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` `setThreadDecorator(ThreadDecorator threadDecorator)` Sets the ThreadDecorator associated this manager should use.
`void` `setThreadFactory(ThreadFactory factory)`
`int` `size()` Answers the number of tasks currently in the queue.
`void` `stop()` Stop the scheduler if it's running.
`java.lang.String` `toString()`

### Methods inherited from class java.util.concurrent.ScheduledThreadPoolExecutor

`decorateTask, decorateTask, execute, getContinueExistingPeriodicTasksAfterShutdownPolicy, getExecuteExistingDelayedTasksAfterShutdownPolicy, getQueue, schedule, schedule, scheduleAtFixedRate, setContinueExistingPeriodicTasksAfterShutdownPolicy, setExecuteExistingDelayedTasksAfterShutdownPolicy, shutdown, shutdownNow, submit, submit, submit`

### Methods 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, terminated`

### Methods inherited from class java.util.concurrent.AbstractExecutorService

`invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor`

### Methods inherited from class java.lang.Object

`clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.jgroups.util.TimeScheduler

`execute, isShutdown, schedule, scheduleAtFixedRate`

### Methods inherited from interface java.util.concurrent.ExecutorService

`awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated`

### Field Detail

#### log

    protected static final Log log

### Constructor Detail

#### DefaultTimeScheduler

    public DefaultTimeScheduler()

Create a scheduler that executes tasks in dynamically adjustable intervals

#### DefaultTimeScheduler

    public DefaultTimeScheduler(ThreadFactory factory)

#### DefaultTimeScheduler

    public DefaultTimeScheduler(ThreadFactory factory,
                        int max_threads)

#### DefaultTimeScheduler

    public DefaultTimeScheduler(int corePoolSize)

### Method Detail

#### setThreadFactory

    public void setThreadFactory(ThreadFactory factory)

Specified by:
  `setThreadFactory` in interface `TimeScheduler`

#### getThreadDecorator

    public ThreadDecorator getThreadDecorator()

Description copied from interface: `ThreadManager`
Gets the ThreadDecorator associated with this manager.

Specified by:
  `getThreadDecorator` in interface `ThreadManager`
Returns:
  the ThreadDecorator, or `null` if there is none.

#### setThreadDecorator

    public void setThreadDecorator(ThreadDecorator threadDecorator)

Description copied from interface: `ThreadManager`
Sets the ThreadDecorator associated this manager should use.

Specified by:
  `setThreadDecorator` in interface `ThreadManager`
Parameters:
  `threadDecorator` - the ThreadDecorator, or `null`.

#### 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 interface `TimeScheduler`
Returns:

#### 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 interface `TimeScheduler`
Returns:

#### getMinThreads

    public int getMinThreads()

Description copied from interface: `TimeScheduler`
Returns the configured core threads, or -1 if not applicable

Specified by:
  `getMinThreads` in interface `TimeScheduler`
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 interface `TimeScheduler`

#### getMaxThreads

    public int getMaxThreads()

Description copied from interface: `TimeScheduler`
Returns the configured max threads, or -1 if not applicable

Specified by:
  `getMaxThreads` in interface `TimeScheduler`
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 interface `TimeScheduler`

#### 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 interface `TimeScheduler`

#### 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 interface `TimeScheduler`

#### scheduleWithDynamicInterval

    public java.util.concurrent.Future<?> scheduleWithDynamicInterval(TimeScheduler.Task task)

Schedule a task for execution at varying intervals. After execution, the task will get rescheduled after `org.jgroups.util.DefaultTimeScheduler.Task#nextInterval()` milliseconds. The task is neve done until nextInterval() return a value <= 0 or the task is cancelled.

Specified by:
  `scheduleWithDynamicInterval` in interface `TimeScheduler`
Parameters:
  `task` - the task to execute
  `relative` - 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 java.util.concurrent.ScheduledFuture<?> scheduleWithFixedDelay(java.lang.Runnable command,
                                                                 long initialDelay,
                                                                 long delay,
                                                                 java.util.concurrent.TimeUnit unit)

Description copied from interface: `TimeScheduler`
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. 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:
  `scheduleWithFixedDelay` in interface `java.util.concurrent.ScheduledExecutorService`
Specified by:
  `scheduleWithFixedDelay` in interface `TimeScheduler`
Overrides:
  `scheduleWithFixedDelay` in class `java.util.concurrent.ScheduledThreadPoolExecutor`
Parameters:
  `command` - the task to execute
  `initialDelay` - the time to delay first execution
  `delay` - the delay between the termination of one execution and the commencement of the next
  `unit` - 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:
  `size` in interface `TimeScheduler`
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:
  `stop` in interface `TimeScheduler`
Throws:
  `java.lang.InterruptedException` - if interrupted while waiting for thread to return

#### afterExecute

    protected void afterExecute(java.lang.Runnable r,
                    java.lang.Throwable t)

Overrides:
  `afterExecute` in class `java.util.concurrent.ThreadPoolExecutor`

#### toString

    public java.lang.String toString()

Overrides:
  `toString` in class `java.lang.Object`