com.sun.sgs.impl.kernel.schedule
Class FIFOSchedulerQueue

java.lang.Object
  extended by com.sun.sgs.impl.kernel.schedule.FIFOSchedulerQueue
All Implemented Interfaces:
TimedTaskListener, SchedulerQueue

public class FIFOSchedulerQueue
extends Object
implements SchedulerQueue, TimedTaskListener

This is a simple implemenation of SchedulerQueue that accepts tasks and runs them in the order that they are ready. No attempt is made to support priority, or to provide any degree of fairness between users. This class uses an un-bounded queue. Unless the system runs out of memory, this should always accept any tasks from any user.


Constructor Summary
FIFOSchedulerQueue(Properties properties)
          Creates an instance of FIFOSchedulerQueue.
 
Method Summary
 void addTask(ScheduledTask task)
          Adds a task to the scheduler.
 RecurringTaskHandle createRecurringTaskHandle(ScheduledTask task)
          Creates a RecurringTaskHandle for the associated task.
 ScheduledTask getNextTask(boolean wait)
          Returns the next task to run, blocking if wait is true.
 int getNextTasks(Collection<? super ScheduledTask> tasks, int max)
          Places at most the next max tasks available into the provided Collection.
 int getReadyCount()
          Returns the number of tasks that are ready to run.
 void notifyCancelled(ScheduledTask task)
          Notifies the scheduler that the given task has been cancelled.
 TaskReservation reserveTask(ScheduledTask task)
          Reserves a space for a task.
 void shutdown()
          Tells this SchedulerQueue to shutdown.
 void timedTaskReady(ScheduledTask task)
          Called when a delayed task has reached its time to run.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FIFOSchedulerQueue

public FIFOSchedulerQueue(Properties properties)
Creates an instance of FIFOSchedulerQueue.

Parameters:
properties - the available system properties
Method Detail

getReadyCount

public int getReadyCount()
Returns the number of tasks that are ready to run. This does not include any pending or recurring tasks that have not yet come time to run.

Specified by:
getReadyCount in interface SchedulerQueue
Returns:
the number of tasks ready to run

getNextTask

public ScheduledTask getNextTask(boolean wait)
                          throws InterruptedException
Returns the next task to run, blocking if wait is true. If not waiting, it returns null if nothing is available.

Specified by:
getNextTask in interface SchedulerQueue
Parameters:
wait - whether to wait for a task to become available
Returns:
the next ScheduledTask or null
Throws:
InterruptedException - if the thread is interrupted while waiting for a task

getNextTasks

public int getNextTasks(Collection<? super ScheduledTask> tasks,
                        int max)
Places at most the next max tasks available into the provided Collection. This method does not block, so it may not provide any tasks. The number of tasks provided is returned.

Note that there is no guarentee that the tasks provided are all contiguous. That is, if two calls are made at the same time to this method, or a call to getNextTask is made at the same time, the scheduler does not have to provide the next max tasks to one consumer before servicing others. If this behavior is needed, the caller needs to synchronize access. The tasks are guaranteed to be in correct order if the provided Collection is ordered.

Specified by:
getNextTasks in interface SchedulerQueue
Parameters:
tasks - the Collection into which the tasks are put
max - the maximum number of tasks to get
Returns:
the number of tasks provided

reserveTask

public TaskReservation reserveTask(ScheduledTask task)
Reserves a space for a task.

Specified by:
reserveTask in interface SchedulerQueue
Parameters:
task - the ScheduledTask to reserve
Returns:
a TaskReservation for the task

addTask

public void addTask(ScheduledTask task)
Adds a task to the scheduler. This task is executed only once, but may be executed immediately or in the future.

Note that if this is a recurring task, then a call to its getRecurringTaskHandle method must return a handle provided by a call to this implementation's createRecurringTaskHandle method. Note also that it is up to the caller to re-schedule this task for each recurrence, and that each such call must provide a unique instance of ScheduledTask.

Specified by:
addTask in interface SchedulerQueue
Parameters:
task - the ScheduledTask to add

createRecurringTaskHandle

public RecurringTaskHandle createRecurringTaskHandle(ScheduledTask task)
Creates a RecurringTaskHandle for the associated task. The associated task will not actually be available through this queue until start is called on the returned handle. The ScheduledTask instance must never have been previously used to schedule a recurring task. Note that after the initial execution of this task, it is up to the caller to schedule each recurrence through a call to addTask.

Specified by:
createRecurringTaskHandle in interface SchedulerQueue
Parameters:
task - the ScheduledTask to run recurringly
Returns:
a RecurringTaskHandle that manages the task

notifyCancelled

public void notifyCancelled(ScheduledTask task)
Notifies the scheduler that the given task has been cancelled. An implementation does not need to do anything in reaction to this call as long as the given task will not run. If this task is unknown, or has already run, this call may be ignored.

Specified by:
notifyCancelled in interface SchedulerQueue
Parameters:
task - the ScheduledTask that has been cancelled

timedTaskReady

public void timedTaskReady(ScheduledTask task)
Called when a delayed task has reached its time to run.

Specified by:
timedTaskReady in interface TimedTaskListener
Parameters:
task - the ScheduledTask that is ready to run

shutdown

public void shutdown()
Tells this SchedulerQueue to shutdown.

Specified by:
shutdown in interface SchedulerQueue

RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved