Reputation: 43
I am currently working on creating a small framework using the executor and completion services to concurrently execute multiple tasks at the same time. I have another thread that runs independently which monitors the completion service and polls futures every 2 seconds to check for results. When a task fails, I would like to re execute it by submitting it back. I would like to retry up to 3 times. I now need a collection that can let me store the task ID, reference of the task object and the number of retries attempted which would be an integer while keeping the task ID as the unique key. Currently I am using 2 hash maps. One for storing the ID and reference and the other for storing the ID and retry count.
Edit My question is that I need suggestions for a collection that would let me store the 3 things by keeping task ID as the unique key to retrieve the task reference and the retry count more efficiently than 2 hash maps.
Upvotes: 3
Views: 949
Reputation: 44918
Define a class to hold the tasks and number of execution attemps:
public class ExecutionAttempt {
private final Task task;
private int numberOfFailedAttempts = 0;
public ExecutionAttempt(Task task) { ... }
public int getNumberOfFailedAttempts() { ... }
public void countFailedAttempt() {
numberOfFailedAttempts++;
}
...
}
Then save the tasks you are currently trying to execute in an ordinary HashMap
:
HashMap<TaskId, ExecutionAttempt> currentlyRunningTaks = ...
You could also get away with Pair<Task, Integer>
, but this could quickly get out of hand as soon as you want to add some additional informations, like Long averageTimeToFailure
or something like that.
I definitely wouldn't worry about the few tiny ExecutionAttempt
objects: managing a thread pool and worrying about a dozen additional ExecutionAttempt
objects is like moving a freight train and worrying about not being able to lift a few additional paper envelopes.
Upvotes: 1