Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.jgroups.util
Class TimeScheduler  view TimeScheduler download TimeScheduler.java

java.lang.Object
  extended byorg.jgroups.util.TimeScheduler

public class TimeScheduler
extends java.lang.Object

Fixed-delay & fixed-rate single thread scheduler

The scheduler supports varying scheduling intervals by asking the task every time for its next preferred scheduling interval. Scheduling can either be fixed-delay or fixed-rate. The notions are borrowed from java.util.Timer and retain the same meaning. I.e. in fixed-delay scheduling, the task's new schedule is calculated as:
new_schedule = time_task_starts + scheduling_interval

In fixed-rate scheduling, the next schedule is calculated as:
new_schedule = time_task_was_supposed_to_start + scheduling_interval

The scheduler internally holds a queue of tasks sorted in ascending order according to their next execution time. A task is removed from the queue if it is cancelled, i.e. if TimeScheduler.Task.isCancelled() returns true.

The scheduler internally uses a java.util.SortedSet to keep tasks sorted. java.util.Timer uses an array arranged as a binary heap that doesn't shrink. It is likely that the latter arrangement is faster.

Initially, the scheduler is in SUSPENDed mode, start() need not be called: if a task is added, the scheduler gets started automatically. Calling start() starts the scheduler if it's suspended or stopped else has no effect. Once stop() is called, added tasks will not restart it: start() has to be called to restart the scheduler.


Nested Class Summary
private static class TimeScheduler.IntTask
          Internal task class.
private  class TimeScheduler.Loop
          The scheduler thread's main loop
static interface TimeScheduler.Task
          The interface that submitted tasks must implement
private static class TimeScheduler.TaskQueue
          The task queue used by the scheduler.
 
Field Summary
protected static org.apache.commons.logging.Log log
           
private  TimeScheduler.TaskQueue queue
          The task queue ordered according to task's next execution time
private static int RUN
          Thread is running

A call to start() has no effect on the thread
A call to stop() will stop the thread
A call to add() has no effect on the thread

private static int STOP
          Thread is stopped

A call to start() will recreate the thread
A call to stop() has no effect on the thread
A call to add() has no effect on the thread

private static int STOPPING
          A shutdown of the thread is in progress

A call to start() has no effect on the thread
A call to stop() has no effect on the thread
A call to add() has no effect on the thread

private static int SUSPEND
          Thread is suspended

A call to start() will recreate the thread
A call to stop() will switch the state from suspended to stopped
A call to add() will recreate the thread only if it is suspended

private  long suspend_interval
          Time that task queue is empty before suspending the scheduling thread
private static long SUSPEND_INTERVAL
          Default suspend interval (ms)
private  java.lang.Thread thread
          The scheduler thread
private static java.lang.String THREAD_NAME
          TimeScheduler thread name
private  int thread_state
          The thread's running state
 
Constructor Summary
TimeScheduler()
          Create a scheduler that executes tasks in dynamically adjustable intervals
TimeScheduler(long suspend_interval)
          Create a scheduler that executes tasks in dynamically adjustable intervals
 
Method Summary
private  void _run()
          If the task queue is empty, sleep until a task comes in or if slept for too long, suspend the thread.
private  void _start()
          Set the thread state to running, create and start the thread
private  void _stop()
          Set the thread state to stopped
private  void _stopping()
          Set the thread state to stopping
private  void _suspend()
          Set the thread state to suspended
private  void _unsuspend()
          Restart the suspended thread
 void add(TimeScheduler.Task t)
          Add a task for execution at adjustable intervals
 void add(TimeScheduler.Task t, boolean relative)
          Add a task for execution at adjustable intervals
 java.lang.String dumpTaskQueue()
           
 long getSuspendInterval()
           
 void setSuspendInterval(long s)
           
 int size()
          Answers the number of tasks currently in the queue.
 void start()
          Start the scheduler, if it's suspended or stopped
 void stop()
          Stop the scheduler if it's running.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SUSPEND_INTERVAL

private static final long SUSPEND_INTERVAL
Default suspend interval (ms)

See Also:
Constant Field Values

RUN

private static final int RUN
Thread is running

A call to start() has no effect on the thread
A call to stop() will stop the thread
A call to add() has no effect on the thread

See Also:
Constant Field Values

SUSPEND

private static final int SUSPEND
Thread is suspended

A call to start() will recreate the thread
A call to stop() will switch the state from suspended to stopped
A call to add() will recreate the thread only if it is suspended

See Also:
Constant Field Values

STOPPING

private static final int STOPPING
A shutdown of the thread is in progress

A call to start() has no effect on the thread
A call to stop() has no effect on the thread
A call to add() has no effect on the thread

See Also:
Constant Field Values

STOP

private static final int STOP
Thread is stopped

A call to start() will recreate the thread
A call to stop() has no effect on the thread
A call to add() has no effect on the thread

See Also:
Constant Field Values

THREAD_NAME

private static final java.lang.String THREAD_NAME
TimeScheduler thread name

See Also:
Constant Field Values

thread

private java.lang.Thread thread
The scheduler thread


thread_state

private int thread_state
The thread's running state


suspend_interval

private long suspend_interval
Time that task queue is empty before suspending the scheduling thread


queue

private final TimeScheduler.TaskQueue queue
The task queue ordered according to task's next execution time


log

protected static final org.apache.commons.logging.Log log
Constructor Detail

TimeScheduler

public TimeScheduler(long suspend_interval)
Create a scheduler that executes tasks in dynamically adjustable intervals


TimeScheduler

public TimeScheduler()
Create a scheduler that executes tasks in dynamically adjustable intervals

Method Detail

_start

private void _start()
Set the thread state to running, create and start the thread


_unsuspend

private void _unsuspend()
Restart the suspended thread


_suspend

private void _suspend()
Set the thread state to suspended


_stopping

private void _stopping()
Set the thread state to stopping


_stop

private void _stop()
Set the thread state to stopped


_run

private void _run()
If the task queue is empty, sleep until a task comes in or if slept for too long, suspend the thread.

Get the first task, if the running time hasn't been reached then wait a bit and retry. Else reschedule the task and then run it.


setSuspendInterval

public void setSuspendInterval(long s)

getSuspendInterval

public long getSuspendInterval()

dumpTaskQueue

public java.lang.String dumpTaskQueue()

add

public void add(TimeScheduler.Task t,
                boolean relative)
Add a task for execution at adjustable intervals


add

public void add(TimeScheduler.Task t)
Add a task for execution at adjustable intervals


size

public int size()
Answers the number of tasks currently in the queue.


start

public void start()
Start the scheduler, if it's suspended or stopped


stop

public void stop()
          throws java.lang.InterruptedException
Stop the scheduler if it's running. Switch to stopped, if it's suspended. Clear the task queue.