## org.jgroups.util
Class TimeScheduler2

    java.lang.Object
      org.jgroups.util.TimeScheduler2

All Implemented Interfaces:
  java.lang.Runnable, ThreadManager, TimeScheduler

    public class TimeScheduler2

extends java.lang.Object
implements TimeScheduler, java.lang.Runnable

Implementation of `TimeScheduler`. Uses a thread pool and a single thread which waits for the next task to be executed. When ready, it passes the task to the associated pool to get executed. When multiple tasks are scheduled to get executed at the same time, they're collected in a queue associated with the task execution time, and are executed together.

Author:
  Bela Ban

Nested Class Summary
`protected static class` `TimeScheduler2.MyTask` Simple task wrapper, always executed by at most 1 thread.

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

Field Summary
`protected static Log` `log`
`protected java.util.concurrent.atomic.AtomicBoolean` `no_tasks` Needed to signal going from 0 tasks to non-zero (we cannot use tasks.isEmpty() here ...)
`protected boolean` `running`
`protected static long` `SLEEP_TIME`
`protected ThreadDecorator` `threadDecorator`
`protected ThreadFactory` `timer_thread_factory`

Constructor Summary
`TimeScheduler2()` Create a scheduler that executes tasks in dynamically adjustable intervals
`TimeScheduler2(int corePoolSize)`
`TimeScheduler2(ThreadFactory factory, int min_threads, int max_threads, long keep_alive_time, int max_queue_size)`

Method Summary
`protected void` `_run()`
`java.lang.String` `dumpTimerTasks()` Returns a list of tasks currently waiting for execution.
`void` `execute(java.lang.Runnable task)` Executes command with zero required delay.
`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
`int` `getQueueSize()`
`ThreadDecorator` `getThreadDecorator()` Gets the ThreadDecorator associated with this manager.
`protected void` `init()`
`boolean` `isShutdown()` Returns true if stop() has been called, false otherwise
`void` `run()`
`java.util.concurrent.Future<?>` `schedule(java.lang.Runnable work, long delay, java.util.concurrent.TimeUnit unit)` Creates and executes a one-shot action that becomes enabled after the given delay.

`java.util.concurrent.Future<?>`

`scheduleAtFixedRate(java.lang.Runnable task, long initial_delay, 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 period; that is executions will commence after `initialDelay` then `initialDelay+period`, then `initialDelay + 2 * period`, and so on.

`java.util.concurrent.Future<?>` `scheduleWithDynamicInterval(TimeScheduler.Task task)` Schedule a task for execution at varying intervals.
`java.util.concurrent.Future<?>` `scheduleWithFixedDelay(java.lang.Runnable task, long initial_delay, 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()` Returns the number of tasks currently in the timer
`protected void` `startRunner()`
`void` `stop()` Stops the timer, cancelling all tasks
`protected void` `stopRunner()`
`protected void` `taskReady(long trigger_time)` Signals that a task with a lower execution time than next_execution_time is ready
`java.lang.String` `toString()`
`protected void` `waitFor()`
`protected void` `waitUntilNextExecution()` Sleeps until the next task in line is ready to be executed

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Field Detail

### no_tasks

    protected final java.util.concurrent.atomic.AtomicBoolean no_tasks

  Needed to signal going from 0 tasks to non-zero (we cannot use tasks.isEmpty() here ...)

### running

    protected volatile boolean running

### log

    protected static final Log log

### threadDecorator

    protected ThreadDecorator threadDecorator

### timer_thread_factory

    protected ThreadFactory timer_thread_factory

### SLEEP_TIME

    protected static final long SLEEP_TIME

See Also:
  Constant Field Values

Constructor Detail

### TimeScheduler2

    public TimeScheduler2()

  Create a scheduler that executes tasks in dynamically adjustable intervals

### TimeScheduler2

    public TimeScheduler2(ThreadFactory factory,
                          int min_threads,
                          int max_threads,
                          long keep_alive_time,
                          int max_queue_size)

### TimeScheduler2

    public TimeScheduler2(int corePoolSize)

Method Detail

### 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`.

### setThreadFactory

    public void setThreadFactory(ThreadFactory factory)

  Specified by:
    `setThreadFactory` in interface `TimeScheduler`

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

### 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:

### getQueueSize

    public int getQueueSize()

### 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:

### execute

    public void execute(java.lang.Runnable task)

  Description copied from interface: `TimeScheduler`
  Executes command with zero required delay. This has effect equivalent to `schedule(command, 0, anyUnit)`.

  Specified by:
    `execute` in interface `TimeScheduler`
  Parameters:
    `task` - the task to execute

### schedule

    public java.util.concurrent.Future<?> schedule(java.lang.Runnable work,
                                                   long delay,
                                                   java.util.concurrent.TimeUnit unit)

  Description copied from interface: `TimeScheduler`
  Creates and executes a one-shot action that becomes enabled after the given delay.

  Specified by:
    `schedule` in interface `TimeScheduler`
  Parameters:
    `work` - the task to execute
    `delay` - the time from now to delay execution
    `unit` - 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 java.util.concurrent.Future<?> scheduleWithFixedDelay(java.lang.Runnable task,
                                                                 long initial_delay,
                                                                 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 `TimeScheduler`
  Parameters:
    `task` - the task to execute
    `initial_delay` - 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

### scheduleAtFixedRate

    public java.util.concurrent.Future<?> scheduleAtFixedRate(java.lang.Runnable task,
                                                              long initial_delay,
                                                              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 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:
    `scheduleAtFixedRate` in interface `TimeScheduler`
  Parameters:
    `task` - the task to execute
    `initial_delay` - the time to delay first execution
    `delay` - the period between successive executions
    `unit` - 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

    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.TimeScheduler2.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 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:
    `size` in interface `TimeScheduler`
  Returns:
    The number of tasks currently in the timer

### toString

    public java.lang.String toString()

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

### stop

    public void stop()

  Stops the timer, cancelling all tasks

  Specified by:
    `stop` in interface `TimeScheduler`
  Throws:
    `java.lang.InterruptedException` - if interrupted while waiting for thread to return

### isShutdown

    public boolean isShutdown()

  Description copied from interface: `TimeScheduler`
  Returns true if stop() has been called, false otherwise

  Specified by:
    `isShutdown` in interface `TimeScheduler`

### run

    public void run()

  Specified by:
    `run` in interface `java.lang.Runnable`

### _run

    protected void _run()

### init

    protected void init()

### waitUntilNextExecution

    protected void waitUntilNextExecution()

  Sleeps until the next task in line is ready to be executed

### waitFor

    protected void waitFor()

### taskReady

    protected void taskReady(long trigger_time)

  Signals that a task with a lower execution time than next_execution_time is ready

### startRunner

    protected void startRunner()

### stopRunner

    protected void stopRunner()

Copyright © 1998-2009 Bela Ban / Red Hat. All Rights Reserved.