2012-03-20 39 views
-1

我有,只要你點擊我的程序的啓動按鈕運行下面的代碼。我通過註釋表示我想要定時器去,問題是,當我做thread.sleep(time)它凍結了我的程序!所以,我想知道如果someoen可以簡單地將atimer添加到我的代碼中,以便它運行第一個位,等待,然後再基於bumpNum運行它。程序不等待定時器繼續它的循環之前完成

代碼:

public class startReplyButtonListener implements ActionListener{ 


     public void actionPerformed(ActionEvent ev){ 

      int length = textAreaReplyMessage.getText().length(); 
      int remLen = 400 - length; 

      String strHTML = neo.get("http://www.neopets.com/neoboards/topic.phtml?topic=" + txtTopicID.getText()); 
      /*strHTML = neo.post("/neoboards/process_topic.phtml?", new String[][] {{"boardType", "topic_id", "board_id", "message", "next", "remLen"}, {"reply", txtTopicID.getText(), "4", textAreaReplyMessage.getText() , "1", ((Integer)remLen).toString()}}); 

      if(strHTML.contains("No topic with ID")){ 
       txtLog.append("Invalid Topic ID! \n"); 
      } 
      else{ 
       txtLog.append("Bumped Topic ID " + txtTopicID.getText() + "\n"); 
      } 
      */ 
      System.out.println(strHTML); 
      bumpNum = 5; 
      wait = Integer.parseInt(textWait1.getText()) * 1000; //converting to miliseconds 

      int i=1; 
      do{ 
       strHTML = neo.post("/neoboards/process_topic.phtml?", new String[][] {{"boardType", "topic_id", "board_id", "message", "next", "remLen"}, {"reply", txtTopicID.getText(), "4", textAreaReplyMessage.getText() , "1", ((Integer)remLen).toString()}}); 

       txtLog.append("Board Bumped. Waiting "+ ((Integer)(wait/1000)).toString() +" Seconds..." + "\n"); 

       //ADD TIMER HERE 
       i++; 



      }while(i <= bumpNum); 

     } 

     } 

我希望實現什麼目標:

用戶表示他們要多少次 「後」(由bumpNum表示),循環會先,後一次:

strHTML = neo.post("/neoboards/process_topic.phtml?", new String[][] {{"boardType", "topic_id", "board_id", "message", "next", "remLen"}, {"reply", txtTopicID.getText(), "4", textAreaReplyMessage.getText() , "1", ((Integer)remLen).toString()}}); 

然後: 根據用戶輸入,它會等待無論多少秒(txtWait1)然後重複上面的發佈代碼,直到它達到bumpNum。

它將與每次它對顛簸(所以程序不能被凍結)以下更新txtLog:

txtLog.append("Board Bumped. Waiting "+ ((Integer)(wait/1000)).toString() +" Seconds..." + "\n"); 
+0

你想達到什麼目的?如果你只是想在每次循環迭代後睡3秒,那麼你不需要線程,只需在循環中休眠即可。您不需要「在一個線程中」來調用Thread.sleep(1000); – 2012-03-20 19:18:55

+0

我希望它通過循環一次,等待等待變量指示的時間,然後重複,直到循環達到其目標運行量。當我執行Thread.sleep時,程序凍結。我在GUI.java類中做這一切 – user1176922 2012-03-20 19:22:14

+0

然後,只需在單個工作線程中完成所有工作,並在該線程中進行正常的睡眠?或者考慮使用Swing定時器http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html – 2012-03-20 20:18:28

回答

2

編輯:

嘆息。好的,現在我明白了。我不知道答案。你正在談論繪製一個GUI元素。我懷疑你想分割一個線程來完成一項工作,然後顯示你正在等待它的GUI顯示。您需要等待線程完成(請參閱下面的我的join代碼),直到GUI元素刷新時才顯示一些結果。

這更多地取決於比睡眠/定時器的GUI代碼。我現在就開始一個新的問題,並解釋!!!不是代碼!但用1000英尺的僞代碼查看你想要的。是這樣的:

我想分叉在[Swing/Android/etc]後臺運行的線程。我想顯示到該線程已經分叉的用戶,我希望用戶界面,以等待線程無嚴寒,然後我希望用戶界面join與線程並顯示結果。

想想這個問題就像我們想起來。預測我們會問的問題。弄清楚我們不瞭解和不瞭解您的環境。

祝你好運。

編輯:

如果你只是想叫睡眠,那麼你就不需要到餐桌一個線程這一點。所有你需要在你的代碼做的是:

try { 
     Thread.sleep(waitingTime); 
     System.out.println(waitingTime); 
    } catch (InterruptedException e) { 
     Thread.currentThread().interrupt(); 
     e.printStackTrace(); 
    } 

這將暫停waitingTime毫秒當前線程(可能是主線程)。


所以你很快分出3個線程,我猜你不想這樣做。如果您正試圖等待每個線程完成,那麼你將不得不做這樣的事情:

Thread thread = new Thread(new Counter(wait)); 
thread.start(); 
thread.join(); 

夫婦的其他意見:

  • 它被認爲是不好的形式在構造函數中啓動一個線程類別:new Thread(this).start();
  • 您正在Runnable內部創建2個線程對象。您應該在Runnable的之外創建一個。往上看。

    Thread myCounter = new Thread(this); << #1 
    public Counter(int waitingTime) { 
        new Thread(this).start();   << #2 
    } 
    
  • 定義的時候,我不會初始化waitingTime = 0;在構造函數初始化。這很混亂。刪除= 0

        int waitingTime;     << remove the =0 here 
        public Counter(int waitingTime) { 
          this.waitingTime = waitingTime; 
    
  • 當你抓住InterruptedException,一定要處理是正確的。一個好的模式是復位中斷標誌和/或退出線程:

    } catch (InterruptedException e) { 
        // resets the interrupt flag cleared by catching the exception 
        Thread.currentThread.interrupt(); 
        // or stops the thread immediately 
        return; 
    } 
    
+0

感謝您的回答,但我只是在學習Java,所以這一切都讓我感到困惑。我會在上面的GUI中發佈我的完整代碼。在我的OP。 – user1176922 2012-03-20 19:28:09

+0

@ user1176922我已經編輯了我的答案,以防止您只是想暫停一秒。 – Gray 2012-03-20 19:34:14

+0

您的編輯代碼凍結了我的程序:/ – user1176922 2012-03-20 20:21:45

0

你通過循環每次開始一個新的線程。而不是在構造函數中創建新線程,請將do/while循環轉換爲常規方法,而不是新線程的運行方法。你正在做的是產生一個實際上睡眠的新線程,但它不是執行循環的線程,因此線程只能像平常一樣繼續。

相關問題