java - 如何停止计时器

标签 java multithreading timer

我有执行以下操作的方法

public void actionPerformed(ActionEvent e) {

    Timer timer = new Timer();
    Object source = e.getSource();

    String stringfromDate = tffromDate.getText();
    String stringtoDate = tftoDate.getText();

    if (source == button) {
        // auto refresh begins
        int delay = 0; // 0 seconds startup delay
        int period = 7000; // x seconds between refreshes
        timer.scheduleAtFixedRate(new TimerTask()
        {
            @Override
            // i still have to truly understand what overide does however
            // netbeans prompted me to put this
            public void run() {

                try {
                    getdata(stringfromDate, stringtoDate);// run get data
                                                            // method
                } catch (IOException | BadLocationException ex) {
                    Logger.getLogger(JavaApplication63.class.getName())
                            .log(Level.SEVERE, null, ex);
                }
            }
        }, delay, period);
    }

    if (source == button1) {
        timer.cancel();

        textarea.setText("");
    }
}

我的 GUI 上有 2 个按钮,一个称为获取信息(按钮),另一个称为清除信息(按钮 1)。 我似乎无法获得清晰的信息(按钮1)来停止计时器并清除文本区域,以便可以执行新的搜索。我似乎无法让这停止帮助赞赏。

最佳答案

考虑对您的代码进行这些更改。主要是代码以不同的方式执行这些操作:

  • 将计时器的声明拉到类中,以便稍后可以取消之前启动的同一计时器。

  • 仅在按下开始按钮时创建一个新计时器。

    //Pulled up for access to make canceable .
    protected Timer timer;
    
    public void actionPerformed(ActionEvent e) {
    
      Object source = e.getSource();
    
      String stringfromDate = tffromDate.getText();
      String stringtoDate = tftoDate.getText();
    
      if (source == button) {
        //Stop existing one before creating new.
        if(timer != null) {
            timer.cancel();
        }
        //Now make new
        timer = new Timer();
    
        // auto refresh begins
        int delay = 0; // 0 seconds startup delay
        int period = 7000; // x seconds between refreshes
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override 
            public void run() {
    
                try {
                    getdata(stringfromDate, stringtoDate);// run get data
                                                            // method
                } catch (IOException | BadLocationException ex) {
                    Logger.getLogger(JavaApplication63.class.getName()).log(Level.SEVERE, null, ex);
    
                }
    
            }
        }, delay, period);
      }
      if (source == button1) {          
        //NULLCHECK
        if(timer != null) {
            timer.cancel();
        }
        textarea.setText("");
      }
    }
    

关于java - 如何停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34232829/

相关文章:

c# - 具有批量生产者的生产者/消费者模式

java - Wowza 流时间测量,无需垃圾邮件附加线程

swift - 我应该使用未命名的 Timer 还是 Dispatch asyncAfter 来延迟 1 次?

ios - 如何防止应用程序进入休眠状态,以便为音乐集成 sleep 模式

java : Find a word in a string and replace it with a new word if its at a specific position

Java 7 String - 子串复杂度

java - AsyncContext 响应与原始传入请求不匹配?

java - 优先队列/堆更新

java - 在 java 中检查另一个线程 boolean 值的性能哪个更好

c# - 我需要在计时器刻度内的按钮单击对象上使用 if 语句吗?