简体   繁体   中英

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?

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.

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