简体   繁体   中英

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. 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:

    @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

_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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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