2017-01-19 4 views
0

私は複数のスレッドがあるアプリケーションを持っています。私はそれらを順番に実行するようにします。私はexecutorServiceをマルチスレッド用に選択します。スレッド(実行方法)にエラーがある場合は、ネットスレッドに移動したいので、最終的に何個のスレッドが正常に完了したかを知ることができます。(サンプル数:executorサービスインターハンドリング処理

メインクラス:

 public class MySampleClass{ 
     ExecutorService executor = Executors.newSingleThreadExecutor(); 
      for(int i=0; i<=100;i++){  
      executor.submit(new ThreadClass()); 
      } 
//After all threads executed now to shutdown executor 
executor.shutdown() 
executor.awaitForTermination(1,Time.MILLISECONDS); 

マイサンプルThreadクラス:

public class ThreadClass implements Runnable{ 
     @override 
     public void run(){ 
      boolean isCompleted= doAction(); 
      if(!isCompleted){ 
        // I want here to stop this thread only..what to do ? 
        //executor.shutdown will stop all other threads 
       } 
     } 
} 

何をすべきかどんな提案?私はそれを間違ってやっていますか?

+0

なぜあなたは単に「復帰」しないのですか? –

+0

これらはスレッドではなく、それらは_tasks_です。タスクとは、実行する必要のある作業のことです。あなたの 'SingleThreadExecutor'はスレッドを使ってタスクを実行するオブジェクトです。 –

答えて

0
Thread.currentThread().interrupt(); 

スレッドを停止しないでください。 reason Thread.stopは廃止予定です。代わりに、現在のスレッドを中断することができます。

0

RunnableではなくCallableを使用できます。その場合、submitメソッドはFuture(http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html)インスタンスを返します。インスタンスでは、呼び出し可能オブジェクトが正しい方法で動作するかどうかを検証できます。ドキュメントには、それを説明する:私は、右のように説明

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#submit(java.util.concurrent.Callable)

希望を。

関連する問題