Reputation: 8955
I am using Java 7.
I have a method I would like to execute in a new thread (asynchronously) and allow the rest of the process to continue. However, I have the following code, but it still executes synchronously.
executeBookingAndApprovalConcurrently(autoBookingEnabled, tripDTO, approvalRequestDetails, approvalRequest, quoteResponse, tripRequirementsResponse, evaluatorsList, evaluationTree, memberId, properties, session);
logger.log(Level.INFO, "Executed booking and approval requests in a new thread." );
and
private void executeBookingAndApprovalConcurrently(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) {
Callable<Void> taskBookAndApprove = new Callable<Void>() {
public Void call() {
executeBookingAndApproval(autoBookingEnabled, tripDTO, approvalRequestDetails, approvalRequest, quoteResponse, tripRequirementsResponse, evaluatorsList, evaluationTree, memberId, properties, session);
return null;
}
};
Future<Void> futureBookAndApprove = executorService.submit(taskBookAndApprove);
try {
futureBookAndApprove.get();
} catch (InterruptedException e) {
logger.log(Level.SEVERE, "InterruptedException Error calling executeBookingAndApproval in a new thread.", e);
} catch (ExecutionException e) {
logger.log(Level.SEVERE, "ExecutionException Error calling executeBookingAndApproval in a new thread.", e);
}
}
and
private void executeBookingAndApproval(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) {
// do stuff that takes a long time
logger.log(Level.INFO, "Called executeBookingAndApproval successfully in a new thread.");
}
I would like it to print:
Executed booking and approval requests in a new thread.
Called executeBookingAndApproval successfully in a new thread.
But it prints in this order:
Called executeBookingAndApproval successfully in a new thread.
Executed booking and approval requests in a new thread.
Upvotes: 0
Views: 195
Reputation: 2874
executeBookingAndApprovalConcurrently should return the Future so that you can call futureBookAndApprove.get();
outside the method. Eg.
final Future<Void> futureBookAndApprove = executeBookingAndApprovalConcurrently(autoBookingEnabled, tripDTO, approvalRequestDetails, approvalRequest, quoteResponse, tripRequirementsResponse, evaluatorsList, evaluationTree, memberId, properties, session);
logger.log(Level.INFO, "Executed booking and approval requests in a new thread." );
try {
futureBookAndApprove.get();
} catch (InterruptedException e) {
logger.log(Level.SEVERE, "InterruptedException Error calling executeBookingAndApproval in a new thread.", e);
} catch (ExecutionException e) {
logger.log(Level.SEVERE, "ExecutionException Error calling executeBookingAndApproval in a new thread.", e);
}
private Future<Void> executeBookingAndApprovalConcurrently(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) {
return executorService.submit(() -> {
executeBookingAndApproval(autoBookingEnabled, tripDTO, approvalRequestDetails, approvalRequest, quoteResponse, tripRequirementsResponse, evaluatorsList, evaluationTree, memberId, properties, session);
return null;
});
}
when you call get()
the method blocks to wait for the response. So whilst the task would have executed asynchronously, it will not appear as you want unless you perform the get()
after the initial logger.log
line.
Upvotes: 1