Step 10: Queuing IdlingTasks
In the previous step, we lost one important piece of logic: our forklifts lost the ability to be idle.
Let’s add this feature back to them, namely, let’s add the IdlingTask
back to our logic.
We need to create and start the IdlingTask
every time a forklift is released and the requests queue is empty.
When a request for a forklift appears, we need to stop one of the IdlingTask
's and pass the task’s forklift to the request.
To implement this logic, we basically can have a queue of IdlingTask
's instead of a queue of available forklifts.
So, in the Dispatcher.java file, we replace the line:
private final Queue<Forklift> availableForklifts = new LinkedList<>();
with the following:
private final Queue<IdlingTask> idlingTasks = new LinkedList<>();
The logic of manipulating the forklifts queue should be changed as follows:
private void addAvailableForklift(Forklift forklift) {
final var request = forkliftRequests.poll();
if (request != null) {
request.accept(forklift, () -> addAvailableForklift(forklift));
} else {
var idlingTask = new IdlingTask(engine, forklift);
idlingTask.start(() -> {});
idlingTasks.add(idlingTask);
}
}
private void requestForklift(BiConsumer<Forklift, Runnable> request) {
final var idlingTask = idlingTasks.poll();
if (idlingTask != null) {
idlingTask.cancel();
final var forklift = idlingTask.getForklift();
request.accept(forklift, () -> addAvailableForklift(forklift));
} else {
forkliftRequests.add(request);
}
}
The loading and unloading logic that we now have is somewhat deficient: it takes way too much time to handle each truck. Thus, to see the impact of our changes, let’s increase the trucks' inter-arrival time.
In the Model.java file, spawnTrucks()
method, we multiply the truck arrival
interval by a factor of 20:
In this line:
engine().scheduleRelative(scenario.getTruckArrivalIntervalMin() * minute(), this::spawnTrucks);
Add * 20
engine().scheduleRelative(scenario.getTruckArrivalIntervalMin() * 20 * minute(), this::spawnTrucks);
Let’s run the simulation and see how forklifts go back to the base node after handling several trucks.