I am learning Java Future API. I want to write a generic Future handler code that gets any value from a Future and passes it to a callback function. To elaborate, if below is a generic function that handles Future objects:
private static <T> void genericFutureHandler(List<Future<T>> futures, CompletionAction completionAction) throws InterruptedException {
for (Future<T> f : futures) {
if (f.isDone()) {
try {
T iter = f.get();
completionAction.showCompletedTaskNumber(iter);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
I want my callback to be like:
class TaskStatusCompletionAction implements CompletionAction {
@Override
public void showCompletedTaskNumber(int taskNumber) {
System.out.println("Task number : "+taskNumber+" completed");
}
}
interface CompletionAction {
void showCompletedTaskNumber(int taskNumber);
}
How can I write/design code that transparently handles all future objects, gets their completed value and hands if off to the provided callback? Is anything like that possible or I am asking wrong question?