我有,只要你點擊我的程序的啓動按鈕運行下面的代碼。我通過註釋表示我想要定時器去,問題是,當我做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");
你想達到什麼目的?如果你只是想在每次循環迭代後睡3秒,那麼你不需要線程,只需在循環中休眠即可。您不需要「在一個線程中」來調用Thread.sleep(1000); – 2012-03-20 19:18:55
我希望它通過循環一次,等待等待變量指示的時間,然後重複,直到循環達到其目標運行量。當我執行Thread.sleep時,程序凍結。我在GUI.java類中做這一切 – user1176922 2012-03-20 19:22:14
然後,只需在單個工作線程中完成所有工作,並在該線程中進行正常的睡眠?或者考慮使用Swing定時器http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html – 2012-03-20 20:18:28