2016-04-13 7 views
0

私は以下のコードがNumberFormatExceptionのスタックトレースを出力しない理由を理解しようとしていますか?Java:ExecutorServiceとCallables、例外をキャッチできない

私はこのような方法で呼び出し可能オブジェクトとExecutorServiceのを使用するのが一般的であるならば、私はGoogleで検索し、私の問題への解決策を見つけることができませんでした...私は見ていないよということは本当に明らかに何かがあるかもしれませんわかりません。

import java.util.ArrayList; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

public class CallablesTest { 

    private final static ArrayList<Callable<Void>> mCallables = new ArrayList<>(); 
    private final static ExecutorService mExecutor = Executors.newFixedThreadPool(4); 

    public static void main(String[] args) throws Exception{ 
     testMethod(); 
    } 

    static void testMethod() throws Exception { 

     mCallables.clear(); 

     for(int i=0; i<4; i++){ 
      mCallables.add(new Callable<Void>() { 

       @Override 
       public Void call() throws Exception { 
        //if (Thread.currentThread().isInterrupted()) { 
        // throw new InterruptedException("Interruption"); 
        //} 
        System.out.println("New call"); 
        Double.parseDouble("a"); 

        return null; 
       } //end call method 

      }); //end callable anonymous class 
     } 
     try { 
      mExecutor.invokeAll(mCallables); 
      mExecutor.shutdown(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

現在、コードのどの部分も「NumberFormatException」をスローしていません。 'ExecutorService'は、非同期に実行される' Callable'によってスローされた例外を直接公開しません。 – Savior

+0

申し訳ありませんが、コードに間違いがありました。現在、NumberFormatExceptionが存在するはずです。私はあなたのコメントを理解していませんが、 "ExecutorServiceは、非同期に実行されるCallablesによってスローされた例外を直接公開しません。" – HollowBastion

+0

'ExecutorService'のポイントは、通常、スレッドプールとして動作するため、異なるスレッド上で' Callable'を実行します。 'Callable'の実行が他のスレッドの1つでExceptionをスローすると、' ExecutorService'は(デフォルトで)それを傍受しません。それはただそれを捨てる。これをすべて設定するには、 'ThreadPoolExecutor'を見てください。 – Savior

答えて

0

は、私は自分の質問への答えを見つけたかもしれない...あなたは将来のオブジェクトがExecutorService.invokeAll ...から返され、その後のtry/catchを呼び出す今後は「取得」囲む得ればと思いますブロックすると例外をキャッチできます

import java.util.ArrayList; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 

public class ThreadTest { 

    private final static ArrayList<Callable<Boolean>> mCallables = new ArrayList<>(); 
    private final static ExecutorService mExecutor = Executors.newFixedThreadPool(4); 

    public static void main(String[] args) throws Exception{ 
     testMethod(); 
    } 

    static void testMethod() throws Exception { 

     mCallables.clear(); 

     for(int i=0; i<4; i++){ 
      mCallables.add(new Callable<Boolean>() { 

       @Override 
       public Boolean call() throws Exception { 
        //if (Thread.currentThread().isInterrupted()) { 
        // throw new InterruptedException("Interruption"); 
        //} 
        System.out.println("New call"); 

        double d = Double.parseDouble("a"); 

        return true; 
       } //end call method 

      }); //end callable anonymous class 
     } 
     try { 
      List<Future<Boolean>> f= mExecutor.invokeAll(mCallables); 
      f.get(1).get(); 
      f.get(2).get(); 
      f.get(3).get(); 
      f.get(0).get(); 

     } catch (Exception e) { 
      String s = e.toString(); 
      System.out.println(s); 
     } 

     mExecutor.shutdown(); 
    } 
} 
関連する問題