I am using Java 1.7.
I am trying to execute two methods concurrently. However, each method has a different return type.
I have the following:
private ExecutorService executorService = Executors.newFixedThreadPool(2);
private void executeConcurrentTasks(final boolean autoBookingEnabled, final TripDTO tripDTO, final ApprovalRequestDetails approvalRequestDetails, final ApprovalRequest approvalRequest, final QuoteResponse quoteResponse, final TripRequirementsResponse tripRequirementsResponse, final List<List<Evaluator>> evaluatorsList, final List<EvaluationApprovalTree> evaluationTree, final Long memberId, final Map<String,Object> properties, final HttpSession session) {
List<Callable<?>> tasks = Arrays.asList(
new Callable<BookServicesResponse>() {
public BookServicesResponse call() throws Exception {
BookServicesResponse autoBookResponse = bookServices(autoBookingEnabled, tripDTO, approvalRequestDetails, approvalRequest, quoteResponse, memberId, properties, session);
return autoBookResponse;
}
},
new Callable<ApprovalResponse>() {
public ApprovalResponse call() throws Exception {
ApprovalResponse approvalResponse = submitApproval(approvalRequest, tripDTO, tripRequirementsResponse, evaluatorsList, evaluationTree, memberId, properties, session);
return approvalResponse;
}
});
List<Future<?>> futures = executorService.invokeAll(tasks); // compile error
for (Future<?> future : futures) {
try {
Object obj = future.get();
if (obj instanceof BookServicesResponse) {
// ...
} else if (obj instanceof ApprovalResponse) {
// ...
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Compile error:
Question
Is there a generic way to handle the call of the two methods, so that the returned list of futures each have a different object?
