Future Task Async Calls hanging while exception occurs
I wrote many Async Future Task Calls one below another in my java program. Giving one sample below
FutureTask<List<ConditionFact>> x = getConditionFacts(final Member member);
FutureTask<List<RiskFact>> x = getRiskFacts(final Member member);
FutureTask<List<SAEFact>> x = getSAEFacts(final Member member);
Now Assume I deliberately create a exception in the second call above and enclose all the 3 calls get() inside one try/catch block I don't see the exception coming to catch block and my java program just stands still. All the 3 method calls are happening simultaneously.
try {
FutureTask<List<ConditionFact>> task = getConditionFacts(member);
// wait for the task to complete and get the result:
List<ConditionFact> conditionFacts = task.get();
FutureTask<List<ConditionFact>> task = getRiskFacts(member);
// wait for the task to complete and get the result:
List<RiskFact> conditionFacts = task.get();
}
catch (ExecutionException e) {
// an exception occurred.
Throwable cause = e.getCause(); // cause is the original exception thrown by the DAO
}
But if i enclose each of the get() in separate try catch blocks i am able to catch them. Why? Also the moment i sta开发者_运维问答rt enclosing each get() method in try catch blocks i lose the multi-threading. The method calls are happening one by one as coded
FutureTask<List<ConditionFact>> task = getConditionFacts(member);
// wait for the task to complete and get the result:
try {
List<ConditionFact> conditionFacts = task.get();
}
catch (ExecutionException e) {
// an exception occurred.
Throwable cause = e.getCause(); // cause is the original exception thrown by the DAO
}
FutureTask<List<ConditionFact>> task = getRiskFacts(member);
// wait for the task to complete and get the result:
try {
List<RiskFact> conditionFacts = task.get();
}
catch (ExecutionException e) {
// an exception occurred.
Throwable cause = e.getCause(); // cause is the original exception thrown by the DAO
}
when i am able to catch the exception i am losing multi threading and when i am able to spawn multi threads I lose the exception.
Any ideas how to handle exception individually and achieve multi threading also at the same time
I was really off on this. The correct answer is to use a an ExecutorService to run the tasks in a thread pool.
ExecutorService executor = Executor.newFixedThreadPool(2);
FutureTask<List<ConditionFact>> task1 = getConditionFacts(member);
FutureTask<List<ConditionFact>> task2 = getRiskFacts(member);
executor.execute(task1);
executor.execute(task2);
// wait for the task to complete and get the result:
try {
List<ConditionFact> conditionFacts = task1.get();
}
catch (ExecutionException e) {
// an exception occurred.
Throwable cause = e.getCause(); // cause is the original exception thrown by the DAO
}
// wait for the task to complete and get the result:
try {
List<RiskFact> conditionFacts = task2.get();
}
catch (ExecutionException e) {
// an exception occurred.
Throwable cause = e.getCause(); // cause is the original exception thrown by the DAO
}
That'll solve the single threaded execution problem. I got the answer here: http://programmingexamples.wikidot.com/futuretask
The ExecutorCompletionService should help solve your problems. Your code needs a queue mechanism to be implemented to handle the results and the ExecutorCompletionService should help. Check it out.
精彩评论