Java 发生异常时,未来任务异步调用挂起

Java 发生异常时,未来任务异步调用挂起,java,multithreading,exception-handling,concurrency,futuretask,Java,Multithreading,Exception Handling,Concurrency,Futuretask,我在java程序中编写了许多异步未来任务调用,一个接一个地调用。下面给出一个例子 FutureTask<List<ConditionFact>> x = getConditionFacts(final Member member); FutureTask<List<RiskFact>> x = getRiskFacts(final Member member); FutureTask<List<SAEFact>> x = g

我在java程序中编写了许多异步未来任务调用,一个接一个地调用。下面给出一个例子

FutureTask<List<ConditionFact>> x = getConditionFacts(final Member member);
FutureTask<List<RiskFact>> x = getRiskFacts(final Member member);
FutureTask<List<SAEFact>> x = getSAEFacts(final Member member);
FutureTask x=getConditionFacts(最终成员);
FutureTask x=getRiskFacts(最终成员);
FutureTask x=GetsAfacts(最终成员);
现在假设我在上面的第二个调用中故意创建了一个异常,并将所有3个调用get()包含在一个try/catch块中,我没有看到catch块出现异常,我的java程序只是静止不动。所有3个方法调用都同时发生

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
        }
试试{
FutureTask任务=getConditionFacts(成员);
//等待任务完成并获得结果:
List conditionFacts=task.get();
FutureTask任务=getRiskFacts(成员);
//等待任务完成并获得结果:
List conditionFacts=task.get();
}
捕获(执行例外){
//发生异常。
Throwable cause=e.getCause();//cause是DAO引发的原始异常
}
但是如果我将每个get()封装在单独的try-catch块中,我就能够捕获它们。为什么?而且,当我开始在try-catch块中封装每个get()方法时,我就失去了多线程功能。方法调用按编码方式一个接一个地进行

      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
    }
FutureTask任务=getConditionFacts(成员);
//等待任务完成并获得结果:
试一试{
List conditionFacts=task.get();
}
捕获(执行例外){
//发生异常。
Throwable cause=e.getCause();//cause是DAO引发的原始异常
}
FutureTask任务=getRiskFacts(成员);
//等待任务完成并获得结果:
试一试{
List conditionFacts=task.get();
}
捕获(执行例外){
//发生异常。
Throwable cause=e.getCause();//cause是DAO引发的原始异常
}
当我能够捕获异常时,我将丢失多线程;当我能够生成多线程时,我将丢失异常


任何关于如何单独处理异常并同时实现多线程的想法,ExecutorCompletionService都有助于解决您的问题。您的代码需要实现队列机制来处理结果,ExecutorCompletionService应该会有所帮助。看看吧。

我对这件事很感兴趣。正确的答案是使用ExecutorService在线程池中运行任务

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
}
ExecutorService executor=executor.newFixedThreadPool(2);
FutureTask task1=getConditionFacts(成员);
FutureTask task2=获取风险事实(成员);
执行人。执行(任务1);
执行人。执行(任务2);
//等待任务完成并获得结果:
试一试{
List conditionFacts=task1.get();
}
捕获(执行例外){
//发生异常。
Throwable cause=e.getCause();//cause是DAO引发的原始异常
}
//等待任务完成并获得结果:
试一试{
List conditionFacts=task2.get();
}
捕获(执行例外){
//发生异常。
Throwable cause=e.getCause();//cause是DAO引发的原始异常
}
这将解决单线程执行问题。我在这里得到了答案: