2016-04-13 69 views
0

我通過「Java教程」甲骨文公佈,我不知道爲什麼我不能夠從「併發」獲得本例中的條件語句工作章執行:爲什麼這個條件語句從不執行?

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

於starttime變量高於此初始化爲:

long startTime = System.currentTimeMillis(); 

耐心變量可以是硬編碼的值(1小時)或值(以秒)傳過來的命令行參數。

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並打印一條消息到屏幕上:

 // 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(); 
     } 

這裏是控制檯輸出的樣品時,我用1第二個命令行參數運行: 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

我注意到,與原始來源相比,type-o也是如此。我認爲可能就是這樣。 –