启用停止计时器

问题描述:

我有我想要刷新屏幕在一些periods.User将选择在组合任务,如果工作是积极的任务的应用程序,计时器会started.So只有一个定时器存在。启用停止计时器

我试图阻止计时器当从combo.Here选择一个新的任务是停止计时器功能。 它似乎不适用于某些案件。但我无法理解案件。 虽然计时器不为空,并且正在运行,但它不会停止。它在程序开始时运行,一段时间后不起作用。

public void stopTimer() { 
    logger.error("Timer is ready to stop: "); 
    if (notifierTimer != null) { 
     logger.error("Coalesce: " + notifierTimer.isCoalesce()); 
     logger.error("Running: " + notifierTimer.isRunning()); 
    } 
    if (notifierTimer != null && notifierTimer.isRunning()) { 
     notifierTimer.stop(); 
     logger.error("Timer stopped for job id"); 
    } 
} 


public void setTimer(final long jobId) { 
    final int timerTriggerTime = 10000; 

    ActionListener listener = new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      graph.trigger(jobId); 
      logger.error("Graph Triggered: " + jobId); 
     } 
    }; 
    /** create Timer */ 
     notifierTimer = new Timer(timerTriggerTime, listener); 
     /** start timer */ 
     notifierTimer.start(); 
     /** run timer for each user specifed time */ 
     notifierTimer.setDelay(timerTriggerTime); 
     logger.error("Timer started for job id" + jobId); 
} 
+0

做所有'Timer'对象做同样的事情,还是每个'Timer'对象都是为了做不同的事情?或者你只是想'Timer'对象恢复到初始状态,当一个新的作业从'JComboBox' – 2012-03-18 14:48:31

+0

选择编辑:每个定时器对象进行同样的事情刚刷新screen.When一个新作业从组合框中选择是否它是活动状态setTimer方法被调用。我添加了setTimer方法声明 – user725455 2012-03-18 16:10:02

+0

'graph.trigger(jobId)'做了什么?这是一项长期运行的任务吗?你典型的timerTriggerTime间隔是多少?鉴于目前提供的信息,我不确定您的问题是否可以回答。 – 2012-03-18 16:32:10

是的我不使用计时器的原因,如像这样的越野车。

public class MyTimer extends Thread { 
    boolean kill = false; 
    int tick = 10; 
    ActionListener al; 

    public MyTimer(int tick, ActionListener al) { 
     this.tick = tick; 
     this.al = al; 
    } 

    public void run() { 
     try { 
      while(!kill) { 
       al.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_PERFORMED,"tick")); 
       Thread.sleep(tick); 
      } 
     } catch (InterruptedException e) { 
     } 
    } 

    public void stop() 
    { 
     kill = true; 
    } 
} 

应该工作...

+1

不正确的建议:-) – mKorbel 2012-03-18 16:25:52

+1

对不起全局变量,但是这个代码违反了这么多的Swing线程规则,它应该被拿回来和射击。投票。 – 2012-03-18 16:28:18

+0

为什么停止功能不起作用。这可能是一个错误,或者这是我的错? – user725455 2012-03-18 16:30:40

这个人是两者的OP和佩里蒙绍:这是一个“工作”摇摆定时器,一个启动和停止的命令的例子。我还没有完成这个程序,因为我试图让它更像MVC,但是运行它,你会发现它的功能很好。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.event.*; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class CountDownTimer extends JPanel { 
    private static final int BL_GAP = 5; 
    private static final int MS_PER_SEC = 1000; 
    private static final int SEC_PER_MIN = 60; 
    private static final int MIN_PER_HR = 60; 
    private static final float DISPLAY_PTS = 54f; 
    private static final String DISPLAY_FORMAT_STR = "%02d:%02d:%02d:%01d"; 
    private static final float SPINNER_FONT_PTS = 16f; 
    public static final int TIMER_DELAY = 50; 
    public static final Color NEGATIVE_COLOR = Color.red; 
    private StartAction startAction = new StartAction(); 
    private ResetAction resetAction = new ResetAction(startAction); 
    private QuitAction quitAction = new QuitAction(); 
    private final Action[] btnActions = { resetAction , startAction , 
     quitAction }; 
    private JSpinner hourSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 10, 
     1)); 
    private JSpinner minuteSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 
     60, 1)); 
    private JSpinner secondSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 
     60, 1)); 
    private JLabel displayField = new JLabel("", SwingConstants.CENTER); 
    private long startTime; 
    private long currentTime; 
    private long deltaTime; 
    private long setTimeToComplete; 
    private boolean negative = false; 
    private Timer timer; 
    private int hours; 
    private int min; 
    private int sec; 
    private int msec; 
    private JFrame frame; 

    public CountDownTimer(JFrame frame) { 
     this.frame = frame; 
     displayField.setFont(displayField.getFont().deriveFont(Font.BOLD, 
      DISPLAY_PTS)); 
     displayField.setBorder(BorderFactory.createLineBorder(Color.blue, 2)); 

     setLayout(new BorderLayout(BL_GAP, BL_GAP)); 
     int eb = 2; 
     setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb)); 
     add(displayField, BorderLayout.NORTH); 
     add(createGuiBody()); 
     showTimeLeft(); 
    } 

    private JPanel createGuiBody() { 
     JPanel bodyPanel = new JPanel(); 
     bodyPanel.setLayout(new BoxLayout(bodyPanel, BoxLayout.PAGE_AXIS)); 
     bodyPanel.add(createSpinnerPanel()); 
     bodyPanel.add(createButtonPanel()); 
     return bodyPanel; 
    } 

    private JPanel createButtonPanel() { 
     JPanel innerBtnPanel = new JPanel(new GridLayout(1, 0, BL_GAP, 0)); 
     for (Action action : btnActions) { 
     innerBtnPanel.add(new JButton(action)); 
     } 
     JPanel btnPanel = new JPanel(new BorderLayout()); 
     btnPanel.add(innerBtnPanel); 
     return btnPanel; 
    } 

    private JPanel createSpinnerPanel() { 
     Font font = hourSpinner.getFont().deriveFont(Font.BOLD, SPINNER_FONT_PTS); 
     hourSpinner.setFont(font); 
     minuteSpinner.setFont(font); 
     secondSpinner.setFont(font); 
     JPanel spinnerPanel = new JPanel(); 
     spinnerPanel.add(new JLabel("Hrs:")); 
     spinnerPanel.add(hourSpinner); 
     spinnerPanel.add(Box.createHorizontalStrut(BL_GAP * 2)); 
     spinnerPanel.add(new JLabel("Min:")); 
     spinnerPanel.add(minuteSpinner); 
     spinnerPanel.add(Box.createHorizontalStrut(BL_GAP * 2)); 
     spinnerPanel.add(new JLabel("Secs:")); 
     spinnerPanel.add(secondSpinner); 

     return spinnerPanel; 
    } 

    private void showTimeLeft() { 
     int oldMin = min; 
     hours = (int) (deltaTime/(MS_PER_SEC * SEC_PER_MIN * MIN_PER_HR)); 
     min = (int) (deltaTime/(MS_PER_SEC * SEC_PER_MIN) % MIN_PER_HR); 
     sec = (int) (deltaTime/(MS_PER_SEC) % SEC_PER_MIN); 
     msec = (int) (deltaTime % MS_PER_SEC); 

     String displayString = String.format(DISPLAY_FORMAT_STR, hours, min, sec, 
      msec/100); 
     displayField.setText(displayString); 

     if (Math.abs(oldMin - min) > 0) { 
     String title = frame.getTitle().replaceAll("\\d", ""); 
     title = String.format("%02d " + title, min); 
     frame.setTitle(title); 
     } 
    } 

    private class ResetAction extends AbstractAction { 
     private StartAction startAction; 

     public ResetAction(StartAction startAction) { 
     super("Reset"); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_R); 
     this.startAction = startAction; 
     } 

     public void actionPerformed(ActionEvent evt) { 
     if (startAction != null 
       && startAction.getValue(NAME).equals(StartAction.STOP)) { 
      startAction.actionPerformed(new ActionEvent(evt.getSource(), 
        ActionEvent.ACTION_PERFORMED, StartAction.STOP)); 
     } else if (timer != null && timer.isRunning()) { 
      timer.stop(); 
     } 
     displayField.setForeground(null); 
     deltaTime = (Integer) hourSpinner.getValue(); 
     deltaTime = MIN_PER_HR * deltaTime 
       + (Integer) minuteSpinner.getValue(); 
     deltaTime = SEC_PER_MIN * deltaTime 
       + (Integer) secondSpinner.getValue(); 
     deltaTime = MS_PER_SEC * deltaTime; 
     showTimeLeft(); 
     negative = false; 
     timer = new Timer(TIMER_DELAY, new TimerListener()); 
     } 
    } 

    private class StartAction extends AbstractAction { 
     public static final String START = "Start"; 
     public static final String STOP = "Stop"; 

     public StartAction() { 
     putValue(MNEMONIC_KEY, KeyEvent.VK_S); 
     putValue(NAME, START); 
     } 

     public void actionPerformed(ActionEvent evt) { 
     if (timer == null) { 
      return; 
     } 
     if (getValue(NAME).equals(START)) { 
      putValue(NAME, STOP); 

      startTime = System.currentTimeMillis(); 
      currentTime = startTime; 
      setTimeToComplete = deltaTime; 
      timer.start(); 
     } else { 
      if (timer != null && timer.isRunning()) { 
       putValue(NAME, START); 
       timer.stop(); 
      } 
     } 
     } 
    } 

    private class QuitAction extends AbstractAction { 
     public QuitAction() { 
     super("Quit"); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_Q); 
     } 

     public void actionPerformed(ActionEvent arg0) { 
     if (timer != null && timer.isRunning()) { 
      timer.stop(); 
     } 
     frame.dispose(); 
     } 
    } 

    private class TimerListener implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent arg0) { 
     currentTime = System.currentTimeMillis(); 
     deltaTime = setTimeToComplete - currentTime + startTime; 

     if (deltaTime < 0) { 
      deltaTime = -deltaTime; 
      if (!negative) { 
       negative = true; 
       displayField.setForeground(NEGATIVE_COLOR); 
      } 
     } 
     showTimeLeft(); 
     } 
    } 

    private static void createAndShowGui() { 
     String title = "Count Down Timer"; 
     title = JOptionPane.showInputDialog("Timer Title?", title); 
     JFrame frame = new JFrame(title); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new CountDownTimer(frame)); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

你可以只使用一个计时器,并根据需要添加/删除监听器? 这种类型的东西适合你吗?

class Example { 
    // try using one timer. 
    Timer timer = new Timer(1000, null); // initial 


    // don't know if you only call these methods 
    // inside the Swing thread. If you do, you can 
    // remove synchronized. 
    public synchronized void startRefresh(int jobId) { 
     // just to make sure nothing is running... 
     stopRefresh(); 

     int triggerTime = 1000; 
     ActionListener listener = ...; 

     timer.setDelay(triggerTime); 
     timer.setInitialDelay(triggerTime); 

     timer.addActionListener(listener); 
     timer.start(); 
    } 

    public synchronized void stopRefresh() { 
     timer.stop(); 

     for (ActionListener listener : timer.getActionListeners()) { 
      timer.removeActionListener(listener); 
     } 
    } 

}