0

私のJavaプログラムでは、多くのAsync Future Task Callが次々と書きました。将来のタスク非同期呼び出しがハングして例外が発生しました

FutureTask<List<ConditionFact>> x = getConditionFacts(final Member member); 
FutureTask<List<RiskFact>> x = getRiskFacts(final Member member); 
FutureTask<List<SAEFact>> x = getSAEFacts(final Member member); 

の下に一つのサンプルを与えることは今1つのtry/catchブロック内で、私は、例外がブロックをキャッチするために来て表示されていない)私が意図的に上記の2番目の呼び出しで例外を作成し、すべての3つの呼び出しは(取得囲み仮定し、私の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 
     } 

しかし、私が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 
    } 

ように、1つずつ起こっています。

個別に例外を処理すると同時にExecutorCompletionServiceはあなたの問題を解決するのに役立つはずです

答えて

2

私は本当にこれをオフにしました。正解は、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 
} 

これは、シングルスレッド実行の問題を解決します。私はここで答えを得た:

+0

プログラムがまだハングするのを見てみましょう... – Shiv

0

でもマルチスレッド化を達成するためにどのように任意のアイデアが。コードでは、結果を処理するために実装するキューメカニズムが必要であり、ExecutorCompletionServiceが役立つはずです。見てみな。

+0

私はその中で – Shiv

関連する問題