Bali Share 1.1.18

oracle.bali.share.thread
Class TaskScheduler

java.lang.Object
  |
  +--oracle.bali.share.thread.TaskScheduler

public class TaskScheduler
extends java.lang.Object

The TaskScheduler object provides a single object on which multiple tasks may be scheduled. Tasks are objects that implement the simple Task interface.

In the past, clients may have created multiple Timers for each task they needed to execute. However, since each Timer creates its own thread, this got to be extremely expensive. TaskSchedulers avoid this problem by using a single thread for all tasks.

By default, all tasks are executed in the same thread, so any particularly lengthy tasks can delay all subsequent tasks. Clients who need to execute expensive tasks, but don't want to block other tasks from running should use the SpawningTaskScheduler subclass or manually create their own Threads within their tasks.

See Also:
Task, Timer

Constructor Summary
TaskScheduler()
          Creates a TaskScheduler.
 
Method Summary
 void addSchedulerListener(SchedulerListener listener)
          Adds a listener for scheduler events.
 void cancel(Task task)
          Removes the given task from the scheduler.
protected  void finalize()
          Cleans up the task scheduler.
static TaskScheduler getDefaultTaskScheduler()
          Returns a default task scheduler.
 java.lang.String getName()
          Gets the name of the scheduler.
 int getPriority()
          Returns the priority of the scheduler.
 boolean isDaemon()
          Tests if this scheduler is a daemon scheduler.
protected  void processSchedulerEvent(SchedulerEvent event)
          Processes a single scheduler event.
protected  void processTask(Task task, long time)
          Processes a task.
 void removeSchedulerListener(SchedulerListener listener)
          Removes a listener for scheduler events.
protected  void runTask(Task task, TaskEvent event)
          Executes a task.
 void schedule(Task task, int delay)
          Queues a task with a given delay.
 void setDaemon(boolean isDaemon)
          Marks this scheduler as either a daemon scheduler or a user scheduler.
 void setName(java.lang.String name)
          Sets the name of the scheduler, used by any threads created by the scheduler.
 void setPriority(int priority)
          Sets the scheduler's priority.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TaskScheduler

public TaskScheduler()
Creates a TaskScheduler.
Method Detail

getDefaultTaskScheduler

public static TaskScheduler getDefaultTaskScheduler()
Returns a default task scheduler. EWT uses this scheduler internally for all of its tasks.

Clients can use this task scheduler for scheduling their own tasks, but should be careful not to overload it with too many tasks or any potentially time-consuming tasks, since any such tasks will block some EWT functions from proceeding.

Clients with expensive tasks or many tasks should usually create their own task scheduler for all of their needs.


schedule

public void schedule(Task task,
                     int delay)
Queues a task with a given delay. If this task is already scheduled, it will be removed first - i.e., any particular Task instance can only be scheduled once.

Tasks can be scheduled with a delay of zero milliseconds. In general, this will schedule the task to be delivered as soon as possible, though after any currently scheduled tasks that are overdue.

Parameters:
task - an object implementing the Task interface.
delay - the number of milliseconds before the task will execute

cancel

public void cancel(Task task)
Removes the given task from the scheduler. Cancelling a task that isn't scheduled does not cause an exception.

setDaemon

public void setDaemon(boolean isDaemon)
Marks this scheduler as either a daemon scheduler or a user scheduler.

This method must be called before any tasks have been scheduled, or an IllegalThreadStateException will be thrown.

Parameters:
isDaemon - if true, marks this scheduler as a daemon scheduler
Throws:
IllegalThreadStateException - if any tasks have been scheduled

isDaemon

public boolean isDaemon()
Tests if this scheduler is a daemon scheduler.

setName

public void setName(java.lang.String name)
Sets the name of the scheduler, used by any threads created by the scheduler.

getName

public java.lang.String getName()
Gets the name of the scheduler.

setPriority

public void setPriority(int priority)
Sets the scheduler's priority. This should not significantly affect the accuracy of the scheduler, but will affect the priority of the executed tasks. Task schedulers use the same priority constants as Threads.

getPriority

public int getPriority()
Returns the priority of the scheduler. Task schedulers use the same priority constants as Threads.

addSchedulerListener

public void addSchedulerListener(SchedulerListener listener)
Adds a listener for scheduler events.
Parameters:
listener - the listener

removeSchedulerListener

public void removeSchedulerListener(SchedulerListener listener)
Removes a listener for scheduler events.
Parameters:
listener - the listener

processTask

protected void processTask(Task task,
                           long time)
Processes a task. Delivers a SchedulerEvent.TASK_RUNNING event, and then calls runTask to actually execute the task.
Parameters:
task - the task to execute
time - the absolute time, in milliseconds, when task expected to be executed.
See Also:
runTask(oracle.bali.share.thread.Task, oracle.bali.share.thread.TaskEvent)

runTask

protected void runTask(Task task,
                       TaskEvent event)
Executes a task.
Parameters:
task - the task to execute
event - the task event to deliver to the task

finalize

protected void finalize()
                 throws java.lang.Throwable
Cleans up the task scheduler.
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - Any exception thrown by a finalize method causes finalization to halt; otherwise, it is ignored.

processSchedulerEvent

protected void processSchedulerEvent(SchedulerEvent event)
Processes a single scheduler event.
Parameters:
event - the scheduler event to process

Bali Share 1.1.18