java - Android Java 定时器()

标签 java android timer timertask

由于 Timer() 的准确性,我使用它,但其工作方式与 PostDelayed Handler 相同。它只被调用一次。这是计时器代码:

    public void setWFT() {
        WFT = new Timer();
        WFT.schedule(new TimerTask() {          
            @Override
            public void run() {
                WFTTimerMethod();
            }
        }, 60000); // 60 seconds delay
    }

    private void WFTTimerMethod() {
        this.runOnUiThread(Timer_Tick);
    }

    private Runnable Timer_Tick = new Runnable() {
        public void run() {
            // My commands here
        }
    };

一旦定时器启动,这只会在 60 秒后调用 run() 一次。有时,我必须取消计时器来更新延迟(替换“60000”值)。要再次启动计时器,我只需使用新的延迟值再次调用 WFT() 来重新创建计时器。

问题是,当我使用以下方法取消计时器时:

WFT.cancel();
WFT.purge();

计时器未启动。 run() 没有在应该执行的时候执行。所以我的问题是我应该使用cancel()和purge()还是只使用cancel()?

谢谢

最佳答案

From the Java APIpurge() 上:

Most programs will have no need to call this method. It is designed for use by the rare application that cancels a large number of tasks. Calling this method trades time for space: the runtime of the method may be proportional to n + c log n, where n is the number of tasks in the queue and c is the number of cancelled tasks.

所以你只需要调用cancel()

关于java - Android Java 定时器(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16061874/

相关文章:

java - 使用 Flying Saucer 从 XHtml 源文本生成 PDF 的代理问题

一个定时器上只能由一个 channel 提供 pwm 信号吗?

Java - 最终变量

java - 在线程中访问类级别变量

android - 在 Android Studio 中安装 Vuforia

android - 如何以编程方式更改维度?

android - 如何在 Eclipse for Android 上使用 cocos2d-x 进行编程的技巧

java - java中的定时器有多精确?

c# - 具有类似于 Timer.Tick 的事件的精确计时器

java - 处理 Kafka Producer 超时异常的指南?