重用定时器 timertask Java

Reuse a timer timertask Java

Soo 使用扩展 timertask 创建了一个计时器。

label_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                label_1.setVisible(false);
                label_2.setVisible(true);
                timer.purge();
                class MyTimeTask extends TimerTask
        {   
            public void run(){
            genReelNumbers();
            laa++;
           if(laa==50){
            timer.cancel();
            timer.purge();
                laa=0;
            label_1.setVisible(true);
            label_2.setVisible(false);}}}
                timer.purge();
                timer.schedule(new MyTimeTask(), 0, 50);}});

但是我收到一个错误消息,计时器已经被取消了!如您所见,我已经尝试使用 purge(),所以它取消了 "canceled" 计时器(不知道这是否有意义)。每次按下标签时,我都想使用这个计时器!有什么想法吗?

取消计时器后,您别无选择,只能创建一个新对象....

首先,这看起来是一个 Swing 应用程序,如果是这样,您不应该 使用 java.util.Timer 和 java.util.TimerTask,因为Swing是单线程的,上面的两个类创建了一个新的线程或者多个线程来实现它们的动作,也就是说那些应该在Swing事件线程上调用的重要代码不会在这个线程上调用。这可能会导致有害的间歇性和难以调试的线程异常被抛出。而是使用 javax.swing.Timer. Then to stop this timer, simply call stop() on it, and to restart it, simply call start() on it. For more on this, please read: How To Use Swing Timers.

例如,我不是 100% 确定您的代码应该做什么,但它可能看起来像:

// warning: code not compile- nor run-tested
label_1.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        label_1.setVisible(false);
        label_2.setVisible(true);

        // assuming a javax.swing.Timer field named timer
        if (timer != null && timer.isRunning()) {
            // if the timer is not null and it's running, stop it:
            timer.stop();
        }

        // TIMER_DELAY is an int constant that specifies the delay between "ticks"
        timer = new Timer(TIMER_DELAY, new ActionListener() {
            @Override  // this method will be called repeatedly, every TIMER_DELAY msecs
            public void actionPerformed(ActionEvent e) {
                genReelNumbers();
                laa++;
                if(laa==50){
                    timer.stop();
                    // timer.purge();
                    laa=0;
                    label_1.setVisible(true);
                    label_2.setVisible(false);
                }
            }
        });
        timer.start();
    }
});

我按照@Hovercraft 的建议改成了javax.swing.Timer

结果是这样的:

//The variable "taxa" is the amount of times that i want it to do the task
    javax.swing.Timer time1 = new javax.swing.Timer(taxa, new ActionListener() {


            public void actionPerformed(ActionEvent e) {

                genReelNumbers();

            }
            });
//starts the timer
         time1.start();


//New timertask
            TimerTask tt = new TimerTask() {

            @Override
            public void run() {
//stops the timer
            time1.stop();
            label_2.setVisible(false);
            label_1.setVisible(true);
            verificarodas();
            }
            };
            Timer time = new Timer(true);

    // the 2000 is how long i want to do the task's
   //if i changed to 3000 it would take 3 seconds (remember it has to be a value on miliseconds) to do the 15 times, and soo on

            time.schedule(tt, 2000);