public class LoadBalancingScheduler extends SchedulerBase<SchedulerBase.SchedulableRecord>
LoadBalancingScheduler understands the time it has to run and distributes this time among the tasks that need to be
run. This scheduler splits the time it is given according to the number of tasks that must be run on this frame. To adjust for
small errors in the running time of tasks, this scheduler recalculates the time it has left after each task is run. This way an
overrunning task will reduce the time that is given to others run in the same frame.
The scheduler takes tasks, each one having a frequency and a phase that determine when it should be run.
f, we perform a dry run of the scheduler for a fixed number of frames into the
future. Rather than executing tasks in this dry run, we simply count how many would be executed. We find the frame with the
least number of running tasks. The phase value for the task is set to the number of frames ahead at which this minimum occurs.
The fixed number of frames is normally a manually set value found by experimentation. Ideally, it would be the least common
multiple (LCM) of all the frequency values used in the scheduler, see ArithmeticUtils.lcmPositive(int, int). Typically,
however, this is a large number and would slow the algorithm unnecessarily (for frequencies of 2, 3, 5, 7, and 11, for example,
we have an LCM of 2310). Despite being a good approach in practice, it has a theoretical chance that it will still produce
heavy spikes, if the lookahead isn't at least as large as the size of the LCM.SchedulerBase.SchedulableRecord| Modifier and Type | Field and Description |
|---|---|
protected int |
frame
The current frame number
|
dryRunFrames, phaseCounters, runList, schedulableRecords| Constructor and Description |
|---|
LoadBalancingScheduler(int dryRunFrames)
Creates a
LoadBalancingScheduler. |
| Modifier and Type | Method and Description |
|---|---|
void |
add(Schedulable schedulable,
int frequency,
int phase)
Adds the
schedulable to the list using the given frequency and phase |
void |
addWithAutomaticPhasing(Schedulable schedulable,
int frequency)
Adds the
schedulable to the list using the given frequency and a phase calculated by a dry run of the
scheduler. |
void |
run(long timeToRun)
Executes scheduled tasks based on their frequency and phase.
|
calculatePhasepublic LoadBalancingScheduler(int dryRunFrames)
LoadBalancingScheduler.dryRunFrames - number of frames simulated by the dry run to calculate the phase when adding a schedulable via
addWithAutomaticPhasing(Schedulable, int)public void addWithAutomaticPhasing(Schedulable schedulable, int frequency)
schedulable to the list using the given frequency and a phase calculated by a dry run of the
scheduler.schedulable - the task to schedulefrequency - the frequencypublic void add(Schedulable schedulable, int frequency, int phase)
Schedulerschedulable to the list using the given frequency and phaseschedulable - the task to schedulefrequency - the frequencyphase - the phasepublic void run(long timeToRun)
timeToRun - the maximum time in nanoseconds this scheduler should run on the current frame.