2016-04-13 19 views
0

私はOracleが発表され、「Javaチュートリアル」を通じて働いていると私は実行するための「同時実行」の章から、この例では、条件文を取得することはできませんよ、なぜ私はよく分からない:この条件文は実行されないのはなぜですか?

if ((System.currentTimeMillis() - startTime > patience) && 
       msgThread.isAlive()) 

のstartTime変数がこの上に初期化される:忍耐変数はハードコードされた値(1時間)または(秒)の値のいずれかであり得る

long startTime = System.currentTimeMillis(); 

は、コマンドライン引数として渡されました。

long patience = 1000 * 60 * 60; // initially 1 hour of patience 

    // if there is a command-line arg 
    if (args.length > 0) { 
     try { 
      // takes command-line arg as seconds 
      patience = Long.parseLong(args[0]) * 1000; 
     } 
     catch (NumberFormatException e) { 
      System.err.println("Invalid commmand-line argument, " + 
       "terminating..."); 
      System.exit(1); 
     } 
    } 

事は、私は、コマンドライン引数の様々なプログラムを実行すると(1秒後に終了しなければならない、私は1を渡した場合でも、私は応答に差を取得していない!)、ではない、のどれも彼らは実際にはその後、msgThreadを中断し、画面にメッセージを印刷することになる、条件文が真であることを得る:私は1、2番目のコマンドライン引数を実行したときにここで

 // if the amount of time passed is more than patience and the 
     // thread is still alive 
     if ((System.currentTimeMillis() - startTime > patience) && 
      msgThread.isAlive()) { 

      // "tired of waiting" to threadMessage 
      threadMessage("Tired of waiting, ending thread."); 
      // interrupt the thread 
      msgThread.interrupt(); 
      // wait indefinitely for it to finish 
      msgThread.join(); 
     } 

は、コンソール出力のサンプルです: enter image description here

コードの完全なコピーですが、これらの結果を理解する助けがあれば幸いです!

/* 
* This program is from Chapter 13 of "The Java Tutorial". It demonstrates 
* some basic Thread funtionality by displaying 4 different messages 
* 4 seconds apart in one Thread, while the other thread periodically 
* checks the total run time of the application to see if it has passed 
* a patience threshold, which is hard-coded as 1 hour but can be 
* input by the user as a commmand-line arg in seconds. If the patience 
* threshold is reached, the main thread interrupts the message thread 
* and displays "finally!" 
*/ 

public class SimpleThreads { 
    /* 
    * Display a message preceded by the name of the current thread. 
    * This will be called periodically throughout the program to 
    * show where we are in the app 
    */ 
    public static void threadMessage(String message) { 
     String threadName = Thread.currentThread().getName(); 
     System.out.format("%s: %s%n", threadName, message); 
    } 

    /* 
    * Private nested class that will be the Runnable object. The 
    * run() method will be called when a Thread starts execution. 
    */ 
    private static class MessageLoop implements Runnable { 
     /* 
     * Prints a message to the console every four seconds. 
     * There are 4 messages total. If interrupted, says so!. 
     */ 
     @Override 
     public void run() { 
      String[] message = {"msg1", "msg2", "msg3", "msg4"}; 
      try { 
       for (int i = 0; i < message.length; i++) { 
        Thread.sleep(4000); // throws exception if interrupted 
        threadMessage(message[i]); 
       } 
      } 
      catch (InterruptedException e) { 
       threadMessage("I wasn't done!"); 
      } 
     } 
    } 

    /* 
    * Gets the amount of time (patience) that the user will wait for the 
    * MessageLoop classes thread to complete. Starts the MessageLoop 
    * Thread and tracks the total time it runs. If it runs more than 
    * the patience allows for, the MessageLoop thread will be 
    * interrupted. 
    */ 
    public static void main(String[] args) throws InterruptedException { 

     long patience = 1000 * 60 * 60; // initially 1 hour of patience 

     // if there is a command-line arg 
     if (args.length > 0) { 
      try { 
       // takes command-line arg as seconds 
       patience = Long.parseLong(args[0]) * 1000; 
      } 
      catch (NumberFormatException e) { 
       System.err.println("Invalid commmand-line argument, " + 
        "terminating..."); 
       System.exit(1); 
      } 
     } 

     // pass message "Starting MessageLoop thread" to threadMessage 
     threadMessage("Starting MessageLoop"); 
     // get the currentTimeMillis and set as start time (long) 
     long startTime = System.currentTimeMillis(); 

     // create a new Thread and pass it the MessageLoop runnable 
     Thread msgThread = new Thread(new MessageLoop()); 
     // start the thread 
     msgThread.start(); 

     // pass "Waiting for msglp thread to finish" to threadMessage 
     threadMessage("Waiting for MessageLoop to finish..."); 

     // in a loop determined by if the thread is still alive 
     while (msgThread.isAlive()) 

      // wait a max of 1 second for thread to finish 
      msgThread.join(1000); 

      // if the amount of time passed is more than patience and the 
      // thread is still alive 
      if ((System.currentTimeMillis() - startTime > patience) && 
       msgThread.isAlive()) { 

       // "tired of waiting" to threadMessage 
       threadMessage("Tired of waiting, ending thread."); 
       // interrupt the thread 
       msgThread.interrupt(); 
       // wait indefinitely for it to finish 
       msgThread.join(); 
      } 

     // "finally" to threadMessage 
     threadMessage("Finally!"); 
    } 
} 
+0

も今、あなたの 'run'方法で拡張forループを使用することができます'(文字列aMessage:メッセージ)のためには、{ ' – bphilipnyc

答えて

6
while (msgThread.isAlive()) msgThread.join(1000); 

おそらくしばらく{を入力するのを忘れてました。 これでスレッドが終了するまで待っています。

試してみてください。

// in a loop determined by if the thread is still alive 
    while (msgThread.isAlive()) { 

     // wait a max of 1 second for thread to finish 
     msgThread.join(1000); 

     // if the amount of time passed is more than patience and the 
     // thread is still alive 
     if ((System.currentTimeMillis() - startTime > patience) && 
      msgThread.isAlive()) { 

      // "tired of waiting" to threadMessage 
      threadMessage("Tired of waiting, ending thread."); 
      // interrupt the thread 
      msgThread.interrupt(); 
      // wait indefinitely for it to finish 
      msgThread.join(); 
     } 
    } 
+0

ああ男は、私はコードを見てきました今日は長すぎる...間違い、それをキャッチしてくれてありがとう!私は何らかの理由でさらに5分間答えを受け入れることができませんが、それができるようになるとすぐにやります。 –

+0

元のソースと比較すると、そのタイプ-oにも気付きました。それはそれかもしれないと思います。 –

関連する問題