我有,只要你点击我的程序的启动按钮运行下面的代码。我通过注释表示我想要定时器去,问题是,当我做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