可重置的 Java 计时器 [英] Resettable Java Timer

查看:28
本文介绍了可重置的 Java 计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 java.utils.Timer 在 java 中具有可重置的时间.我需要设置一次关闭事件以在 X 秒内发生.如果在创建计时器和 X 秒之间没有发生任何事情,则事件将正常发生.

I'd like to have a java.utils.Timer with a resettable time in java.I need to set a once off event to occur in X seconds. If nothing happens in between the time the timer was created and X seconds, then the event occurs as normal.

但是,如果在 X 秒过去之前,我决定事件应该在 Y 秒后发生,那么我希望能够告诉计时器重置其时间,以便事件在 Y 秒内发生.例如.计时器应该能够执行以下操作:

If, however, before X seconds has elapsed, I decide that the event should occur after Y seconds instead, then I want to be able to tell the timer to reset its time so that the event occurs in Y seconds. E.g. the timer should be able to do something like:

Timer timer = new Timer();  
timer.schedule(timerTask, 5000); //Timer starts in 5000 ms (X)

//At some point between 0 and 5000 ms...  
setNewTime(timer, 8000);  //timerTask will fire in 8000ms from NOW (Y).

我没有看到使用 utils 计时器执行此操作的方法,就像您调用 cancel() 一样,您无法再次安排它.

I don't see a way to do this using the utils timer, as if you call cancel() you cannot schedule it again.

我接近复制此行为的唯一方法是使用 javax.swing.Timer 并涉及停止原始计时器并创建一个新计时器.即:

The only way I've come close to replicating this behavior is by using javax.swing.Timer and involves stopping the origional timer, and creating a new one. i.e.:

timer.stop();
timer = new Timer(8000, ActionListener);
timer.start();

有没有更简单的方法??

Is there an easier way??

推荐答案

根据Timer 文档,从 Java 1.5 开始,您应该更喜欢 ScheduledThreadPoolExecutor.(您可能喜欢使用 Executors.newSingleThreadScheduledExecutor() 以便于使用;它创建的东西很像 Timer.)

According to the Timer documentation, in Java 1.5 onwards, you should prefer the ScheduledThreadPoolExecutor instead. (You may like to create this executor using Executors.newSingleThreadScheduledExecutor() for ease of use; it creates something much like a Timer.)

很酷的事情是,当你安排一个任务时(通过调用 schedule()),它返回一个 ScheduledFuture 对象.您可以使用它来取消计划任务.然后,您可以自由提交具有不同触发时间的新任务.

The cool thing is, when you schedule a task (by calling schedule()), it returns a ScheduledFuture object. You can use this to cancel the scheduled task. You're then free to submit a new task with a different triggering time.

ETA:链接到的 Timer 文档没有说明关于 ScheduledThreadPoolExecutor 的任何内容,但是 OpenJDK 版本有这样的说法:

ETA: The Timer documentation linked to doesn't say anything about ScheduledThreadPoolExecutor, however the OpenJDK version had this to say:

Java 5.0 引入了 java.util.concurrent 包和其中的并发实用程序之一是ScheduledThreadPoolExecutor 这是一个线程池,用于重复以给定的速率或延迟执行任务.它有效地是一个更Timer/TimerTask 的多功能替代品组合,因为它允许多个服务线程,接受各种时间单位,并且不需要子类化 TimerTask(只是实现 Runnable).配置ScheduledThreadPoolExecutor 一个线程使得它等价于定时器.

Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.

这篇关于可重置的 Java 计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆