Java プログラムで、多くの Async Future Task Calls を次々と書きました。以下に1つのサンプルを与える
FutureTask<List<ConditionFact>> x = getConditionFacts(final Member member);
FutureTask<List<RiskFact>> x = getRiskFacts(final Member member);
FutureTask<List<SAEFact>> x = getSAEFacts(final Member member);
ここで、上記の 2 番目の呼び出しで意図的に例外を作成し、get() の 3 つの呼び出しすべてを 1 つの 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
}
しかし、get() のそれぞれを個別の try catch ブロックで囲むと、それらをキャッチできます。なんで?また、各 get() メソッドを try catch ブロックで囲み始めた瞬間に、マルチスレッドが失われます。メソッド呼び出しは、コーディングされているように 1 つずつ発生しています。
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
}
例外をキャッチできるとマルチスレッドが失われ、マルチスレッドを生成できると例外が失われます。
例外を個別に処理し、同時にマルチスレッドを実現する方法についてのアイデア