2012-03-20 83 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循环转换为常规方法,而不是新线程的运行方法。你正在做的是产生一个实际上睡眠的新线程,但它不是执行循环的线程,因此线程只能像平常一样继续。

相关问题