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

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

问题描述

我希望在java中有一个带有可重置时间的java.utils.Timer。我需要在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();

有更简单的方法吗?

推荐答案

根据 计时器 文档,在Java 1.5及更高版本中,您应该更喜欢 ScheduledThreadPoolExecutor 。 (您可能希望使用 <$创建此执行程序c $ c>执行者 .newSingleThreadScheduledExecutor()易于使用;它创建的东西很像计时器。)

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:链接的计时器文档没有说明 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 package和
其中一个并发实用程序是
ScheduledThreadPoolExecutor 这是一个线程池,用于以给定的速率重复
执行任务或延迟。对于计时器 / 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天全站免登陆