简体   繁体   English

以可变速率调度 Java 计时器

[英]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 . Michael评论是正确的:使用schedule而不是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.而且,另一件事: Timer / TimerTask类已被 Executors 框架取代,如 Javadoc 中所述。 I suggest you learn about using a ScheduledExecutorService .我建议你学习使用ScheduledExecutorService Read The Java Tutorials by Oracle, and search Stack Overflow to learn more.阅读 Oracle的 Java 教程,并搜索 Stack Overflow 以了解更多信息。

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

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