2016-03-22 22 views
1

特定の時間後にスレッドを停止したい。ここで私のサンプルコードを追加しました。スレッドのいずれかが6秒以上かかる場合は、特定のスレッドを停止することを意味します。残りのスレッドは影響を受けてはいけません。私はタイマーを使う必要がありますか?もし私がタイマー手段を使用したとしたら、各スレッドの新しいタイマーを作成する必要がありますか?ジョブが6秒前に終了したら、タイマーをキャンセルする必要がありますか?特定の時間の後にスレッドを停止する

class MyRunnableThread implements Runnable{ 

    public static int myCount = 0; 
    private String fname = null; 
    public MyRunnableThread(String fname){ 
     this.fname = fname; 
    } 
    public void run() { 

      try{ 
       //read the content based on text and peforms the operation 
       //File.read(this.fname) 
       System.out.println("Run method stuff"+ this.fname); 
      } catch (Exception iex) { 
       System.out.println("Exception in thread: "+iex.getMessage()); 
      } 
    } 

} 

public class RunMyThread { 

    public static void main(String a[]){ 
     System.out.println("Starting Main Thread..."); 

     String[] fname = {"file1.txt","file2.txt","file3.txt","file4.txt","file5.txt"}; 
     for(int i=0;i<5;i++){ 
      try{ 
       MyRunnableThread mrt = new MyRunnableThread(fname[i]); 
       Thread t = new Thread(mrt); 
       System.out.println("Main Thread start "+i); 
       t.start(); 

       //stop the thread support run method takes more than 6seconds 
       System.out.println("Main Thread end "+ i); 
      } catch (Exception iex){ 
       System.out.println("Exception in main thread: "+iex.getMessage()); 
      } 
     } 
     System.out.println("End of Main Thread..."); 
    } 
} 
+0

6秒前に>始めたスレッドを停止してい(https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html)+ ['Future'](https://docs.oracle.com/javase/8/) docs/api/java/util/concurrent/Future.html)は、そのような目的のために特別に設計されており、大きな機能を提供しています。 –

答えて

0

ユーザータイマーおよび停止するスレッドを伝えるためのフラグ:

public class MyRunnableThread implements Runnable { 

    boolean run = true; 

    String fname; 

    public MyRunnableThread(String fname) { 
     this.fname = fname; 
    } 

    public void setRun(boolean toSet) { 
     run = false; 
    } 

    public void run() { 
     while (run) { 
      doStuff(); 
     } 
    } 

    private void doStuff() { 
//  File.read(fname); 
    } 
} 

どのようにそれを使用する方法:あなたが

をしたい秒で何時間に

import java.util.Timer; 
import java.util.TimerTask; 

public class Tester { 

    public static void main(String args[]) { 

     String[] fname = { "file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt" }; 
     for (int i = 0; i < 5; i++) { 
      try { 

       MyRunnableThread mrt = new MyRunnableThread(fname[i]); 
       Thread t = new Thread(mrt); 
       t.start(); 

       final Timer timer = new Timer(); 
       timer.scheduleAtFixedRate(new TimerTask() { 
        int i = 6; // Time in seconds 

        public void run() { 
         System.out.println(i--); 
         if (i < 0) { 
          timer.cancel(); 
          mrt.setRun(false); 
         } 
        } 
       }, 0, 1000); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

が変化するI

+0

より良い解決方法で編集 – Tim

+0

これは、1ポイントで6秒以上実行されているすべてのスレッドを停止するか、個々のスレッドを停止させますか?明らかに、タイマobjがmrt objと緊密に結合されていますか? mrtとtimerは、それぞれのループごとに新しいobjです。 3番目のobjタイマーはmrtの3番目のオブジェクトを停止しますか? – user1023675

+0

6つのスレッドと6つのタイマーがあり、各スレッドには独自のものがあります。しかし、同時に起動すると、6秒後にほぼ同じ時間に終了します。 – Tim

0

まあ、それを行う方法はたくさんありますが、そのうちの1つを説明します。

あなたがrun()CancelCurrentThread()、あなたの実行がThreadCancelCurrentThread()が、それは例外を無視して停止します開始するメソッドをextends Threadそのカスタムクラスを作成して実装することができます。一つの考えはすべてのあなたのスレッドを格納するArrayListを持っているだろう

mCustomThread thread = new mCustomThread(parametres_if_you_need); 
thread.start(); //Start the Thread 
thread.join(6000); //Adding 6 seconds 
//You can detect if the 6 seconds have passed or not for example using a timer, then if it joins on this if condition you can call 
thread.CancelCurrentThread(); 
+0

私はあなたの返信についてはっきりしていません、あなたはサンプルを提供できますか? – user1023675

1

次に、あなたのコードは次のようになります。

次にあなたが

(タイムスタンプが開始)、各スレッドが余分なフィールドがあることを確認してくださいそして、あなたはタイマーがArrayListのをループして、[ `ExecutorService`]を使用することを検討して

関連する問題