1

ThreadPool Executor を使用しており、invokeAll() メソッドでタスクを呼び出します。呼び出し可能な call() メソッドでは、データベースに書き込みますが、スレッドが正常に実行され、ひどく終了しなかったことが確実な場合にのみ書き込みたいと思います。

この解決策のいずれかが機能しますか:

解決策 1

データベースの書き込み後にスレッドが中断される可能性があり、結果が返されないため、これは機能しないと思います。

public Result call() throws exception(){

    ...

    if( !Thread.currentThread.isInterrupted() ){
       //Save to the database
    } 

    ...

    return result;
}

解決策 2

get() メソッドの後に書き込みを処理して、正常に終了したことを確認します。get() メソッドが実行中にキャッチされた例外を再スローすることを正しく理解していれば、

...

for( Future f : futures){

   try {

       Result r = f.get();

       //Do the write with the result i got
   }
   catch( Exception e){
     //Something went wrong
   }

}

前もって感謝します。

4

1 に答える 1

0

呼び出し可能な call() メソッドで、データベースに書き込みます...

call()その場合、例外処理ロジックはメソッド自体にある必要があります。例えば

public Result call() {
    try {
       ...
       if (!Thread.currentThread.isInterrupted()) {
          // Save to the database
          return result;
       } else {
          return // some result that means interrupted.
       }
    catch (Exception ex) {
       return // some result that means failed.
    }
}

メインスレッドにデータベースの保存をさせようとしても意味がないようです。

于 2013-09-08T03:42:17.560 に答える