FileDocCategorySizeDatePackage
TaskScheduler.javaAPI DocExample2139Mon Nov 13 12:11:54 GMT 2006collections

TaskScheduler

public class TaskScheduler extends Object

Fields Summary
private List
schedule
private final int
FORWARD_PLANNING_DAYS
Constructors Summary
public TaskScheduler()

    
    List<StoppableTaskQueue> temp = new ArrayList<StoppableTaskQueue>();
    for (int i = 0 ; i < FORWARD_PLANNING_DAYS ; i++) {
      temp.add(new StoppableTaskQueue());
    }
    schedule = new CopyOnWriteArrayList<StoppableTaskQueue>(temp);    //1
  
Methods Summary
public voidaddTask(PriorityTask task, int day)

    if (day < 0 || day >= FORWARD_PLANNING_DAYS)
      throw new IllegalArgumentException("day out of range");
    StoppableTaskQueue daysTaskQueue = schedule.get(day);
    if (daysTaskQueue.addTask(task)) return;                        //2
    // StoppableTaskQueue.addTask returns false only when called on
    // a queue that has been shut down. In that case, it will also
    // have been removed by now, so it's safe to try again.
    if (! schedule.get(0).addTask(task)) {
      throw new IllegalStateException("failed to add task " + task);
    }
  
java.util.ListIteratorgetSubSchedule(int startDay, int endDay)

    return schedule.subList(startDay, endDay).listIterator();
  
public PriorityTaskgetTask()

    for (StoppableTaskQueue daysTaskQueue : schedule) {
      PriorityTask topTask = daysTaskQueue.getTask();
      if (topTask != null) return topTask;
    }
    return null;    // no outstanding tasks - at all!?
  
public voidrollOver()

    StoppableTaskQueue oldDay = schedule.remove(0);
    Collection<PriorityTask> remainingTasks = oldDay.shutDown();
    StoppableTaskQueue firstDay = schedule.get(0);
    for (PriorityTask t : remainingTasks) {
      firstDay.addTask(t);
    }
    StoppableTaskQueue lastDay = new StoppableTaskQueue();
    schedule.add(lastDay);