简体   繁体   English

Java倒数计时器未运行TimerTask

[英]Java countdown timer doesn't run TimerTask

I am attempting to run a countdown timer, but for some odd reason, it doesn't repeat the count. 我正在尝试运行倒数计时器,但由于某些奇怪的原因,它不重复计数。 When I put a breakpoint on the Tick() method, the breakpoint is not even hit. 当我在Tick()方法上放置一个断点时,甚至没有命中该断点。 Here's the code: 这是代码:

package mathgame.Models;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
 *
 * @author devinbost
 */
public class CountdownTimer {
    private int _totalSeconds;
    public static int _remainingSeconds;
    private Timer _timer;
    private int _delay = 1000;  // milliseconds
    private int _period = 1000; // milliseconds
    private boolean _isTimeRemaining;
    private List<PropertyChangeTypedListener> listener = new ArrayList<PropertyChangeTypedListener>();

    public CountdownTimer(int totalSeconds){
        _totalSeconds = totalSeconds * 1000;
        _isTimeRemaining = true;
    }
    public void setCountdownTimeLimit(int seconds){
        _totalSeconds = seconds;
        _remainingSeconds = seconds;
    }
    public int getCountdownTimeLimit(){
        return _totalSeconds;
    }
    public int getRemainingSeconds(){
        return _remainingSeconds;
    }
    public void ResetCountdown(){
        _remainingSeconds = this.getCountdownTimeLimit();
        _isTimeRemaining = true;
    }
    public boolean getIsTimeRemaining(){
        return _isTimeRemaining;
    }
    public void StartCountdown(){
        this.ResetCountdown();
        // fire event that starts countdown process.
        _timer = new Timer(false);
        System.out.println("Timer starting countdown. ");
        _timer.schedule(new TimerTask(){
            @Override
            public void run(){
                System.out.println("Test");
                Tick();
            }
        }, 1000); // also fails with: }, 1000, _period);
        System.out.println("Testing");

    }

    public void StopTimer(){
        this._timer.cancel();
        System.out.println("Timer ending countdown. ");
    }
    public void OutOfTime(){
        // do something like fire an event so we can update the gamer's score.
        this.StopTimer();
        boolean isTimeRemaining = this.getIsTimeRemaining();
        this._isTimeRemaining = false;
        // Notify listeners that time ran out.
        this.notifyIsTimeRemainingListeners(this, "_isTimeRemaining", isTimeRemaining, this.getIsTimeRemaining());
        System.out.println("Timer is out of time. ");
    }
    public int Tick() {
//        if (this.getRemainingSeconds() == 1)
//            this.OutOfTime();
        // We need to raise an event to indicate that the value of this.getRemainingSeconds() has changed.
        int priorRemainingSeconds = this.getRemainingSeconds();
        _remainingSeconds--;
        this.notifyCountdownListeners(this, "_remainingSeconds", priorRemainingSeconds, this.getRemainingSeconds());
        return this.getRemainingSeconds();
    }
    // need to make this observable.
    private void notifyCountdownListeners(Object object, String property, int oldValue, int newValue) {
        for (PropertyChangeTypedListener name : listener) 
        {
          name.propertyChange(new PropertyChangeTypedEvent(this, property, oldValue, newValue, EventTypeEnum.CountdownTick));
        }
    }
    private void notifyIsTimeRemainingListeners(Object object, String property, boolean oldValue, boolean newValue) {
        for (PropertyChangeTypedListener name : listener) 
        {
          name.propertyChange(new PropertyChangeTypedEvent(this, property, oldValue, newValue, EventTypeEnum.CountdownOutOfTime));
        }
    }
    public void addChangeListener(PropertyChangeTypedListener newListener) {
        listener.add(newListener);
    }
}

Here's my JUnit test method: 这是我的JUnit测试方法:

    @Test
    public void CountdownTimer_IsTimerTickWorkingProperly_ReturnsTrue(){
        CountdownTimer timer = new CountdownTimer(1000);
        timer.StartCountdown();

    }

Here's the console output: 这是控制台输出:

Timer starting countdown. 计时器开始倒计时。 Testing 测试中

You are missing the period. 这是因为丢失的时间。 schedule(TimerTask task, long delay, long period) Timer API 调度(TimerTask的任务,长时间的延迟,周期长) 定时器API

_timer = new Timer(false);
System.out.println("Timer starting countdown. ");
_timer.schedule(new TimerTask(){
    @Override
    public void run(){
        System.out.println("Test");
        Tick();
    }
}, 1000, _period);
  • TIP: It's advisable that you do not use "_" at the beginning of variables ref here. 提示:这是可取的,你的变量不使用“_”开头参考这里。 and your methods should not start by upper cases either ref here. 和你的方法不应由上面情况下,不是开始参考这里。

UPDATE (This works for me, I think you may have a problem somewhere else in the code, not in the timer) UPDATE(这对我的作品,我觉得你可以在代码中有一个问题在其他地方,而不是在定时器)

import java.util.Timer;
import java.util.TimerTask;
/**
 *
 * @author devinbost
 */
public class CountdownTimer {
    private int _totalSeconds;
    public static int _remainingSeconds;
    private Timer _timer;
    private int _delay = 1000;  // milliseconds
    private int _period = 1000; // milliseconds
    private boolean _isTimeRemaining;
    public CountdownTimer(int totalSeconds){
        _totalSeconds = totalSeconds * 1000;
        _isTimeRemaining = true;
    }    
    public static void main(String[] args) {
         new CountdownTimer(5).StartCountdown();
    }
    public void StartCountdown(){
        this.ResetCountdown();
        // fire event that starts countdown process.
        _timer = new Timer(false);
        System.out.println("Timer starting countdown. ");
        _timer.schedule(new TimerTask(){
            @Override
            public void run(){
                System.out.println("Test");
                //Tick();
            }
        }, 1000, _period);
        System.out.println("Testing");
    }
    public int getCountdownTimeLimit(){
        return _totalSeconds;
    }
    public void ResetCountdown(){
        _remainingSeconds = this.getCountdownTimeLimit();
        _isTimeRemaining = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM