Java倒计时器不运行TimerTask

时间:2014-07-10 20:46:40

标签: java junit javabeans timertask propertychangelistener

我正在尝试运行一个倒数计时器,但由于一些奇怪的原因,它不会重复计数。当我在Tick()方法上放置一个断点时,断点甚至都没有被击中。这是代码:

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);
    }
}

这是我的JUnit测试方法:

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

    }

这是控制台输出:

  

计时器开始倒计时。测试

1 个答案:

答案 0 :(得分:0)

你错过了这个时期。 计划(TimerTask任务,长延迟,长期) Timer 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);
  • 提示:建议您不要使用&#34; _&#34;在变量ref的开头。并且你的方法不应该由大写字母ref开始。

更新(这对我有用,我认为您可能在代码中的其他位置出现问题,而不是在计时器中出现问题)

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;
    }
}