0

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
    }

例外をキャッチできるとマルチスレッドが失われ、マルチスレッドを生成できると例外が失われます。

例外を個別に処理し、同時にマルチスレッドを実現する方法についてのアイデア

4

2 に答える 2

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
}

これにより、シングル スレッド実行の問題が解決されます。ここで答えを得ました:http://programmingexamples.wikidot.com/futuretask

于 2011-07-15T01:53:56.623 に答える
0

ExecutorCompletionService は、問題の解決に役立つはずです。結果を処理するためにコードにキュー メカニズムを実装する必要があり、ExecutorCompletionService が役立ちます。見てみな。

于 2011-07-15T01:53:26.360 に答える