Artsiom Siarheyeu
Artsiom Siarheyeu

Reputation: 5

Schedule java timer at a variable rate

int tickRate = 1950;
TimerTask timerTask = new TimerTask() {
        @Override public void run() { Platform.runLater(new Runnable() {
            @Override
            public void run() {
                if(tickRate > 750) { tickRate -= 100; }
                else tickRate = 750;
                new Egg(gridPane);
            }}); } };
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(timerTask, 1000, tickRate);

How can I make my timer change its rate dynamically?

Upvotes: 0

Views: 322

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338496

The Comment by Michael is correct: Use schedule rather than scheduleAtFixedRate. During each execution of the task, have the task schedule the next execution.

And, another thing: The Timer/TimerTask classes have been supplanted by the Executors framework as noted in the Javadoc. I suggest you learn about using a ScheduledExecutorService. Read The Java Tutorials by Oracle, and search Stack Overflow to learn more.

Upvotes: 2

Related Questions