如何在Java中删除/ postDelayed runnables?

时间:2021-08-15 14:00:11

Upon an event, I'd like to run a task/runnable but delay it's execution in 2 seconds.

在一个事件发生时,我想运行一个任务/可运行但在2秒内延迟它的执行。

During these 2 seconds, if the same event occurs, I'd like to remove the previous task and re-post it to run - again delayed by 2 seconds.

在这2秒内,如果发生相同的事件,我想删除上一个任务并重新发布它 - 再次延迟2秒。

An example scenario would be background compilation. When a file is saved, I'm waiting 2 seconds and start compiling the class and other, depending classes. I don't want to do it all the time - especially if there are editors that save files automatically, like IntelliJ IDEA.

示例场景是后台编译。保存文件时,我等待2秒钟并开始编译类和其他类,具体取决于类。我不想一直这样做 - 特别是如果有编辑器自动保存文件,比如IntelliJ IDEA。

So, how can I remove/postDelayed runnables in Java, like Android's Handler (remove / postDelayed)?

那么,如何在Java中删除/ postDelayed runnables,如Android的Handler(删除/ postDelayed)?

3 个解决方案

#1


you can use the Executors.newScheduledThreadPool in order to schedule the task, and you can follow this post's answer :

你可以使用Executors.newScheduledThreadPool来安排任务,你可以按照这篇文章的答案:

BlockingQueue<Runnable> queue = threadPool.getQueue();

in order to get the queued runnables.

为了得到排队的runnables。

#2


To delay any task and "restart" the delay, you can use Swing timer and if it is running, use its method restart.

要延迟任何任务并“重新启动”延迟,您可以使用Swing计时器,如果它正在运行,请使用其方法restart。

#3


I suppose you could use the ScheduledExecutor to accomplish this, by adding a mechanism to replace queued tasks with re-queued ones. You could look into the source code of ScheduledThreadPoolExecutor to give an idea, perhaps overriding the implementation of ScheduledFuture<?> schedule( Runnable command, long delay, TimeUnit unit ) and figuring out to avoid the scheduling, only running the tasks once.

我想你可以使用ScheduledExecutor来实现这一点,方法是添加一个机制来将排队的任务替换为重新排队的任务。您可以查看ScheduledThreadPoolExecutor的源代码以提供一个想法,可能会覆盖ScheduledFuture schedule(Runnable命令,长延迟,TimeUnit单元)的实现并找出以避免调度,只运行一次任务。

Cheers,

#1


you can use the Executors.newScheduledThreadPool in order to schedule the task, and you can follow this post's answer :

你可以使用Executors.newScheduledThreadPool来安排任务,你可以按照这篇文章的答案:

BlockingQueue<Runnable> queue = threadPool.getQueue();

in order to get the queued runnables.

为了得到排队的runnables。

#2


To delay any task and "restart" the delay, you can use Swing timer and if it is running, use its method restart.

要延迟任何任务并“重新启动”延迟,您可以使用Swing计时器,如果它正在运行,请使用其方法restart。

#3


I suppose you could use the ScheduledExecutor to accomplish this, by adding a mechanism to replace queued tasks with re-queued ones. You could look into the source code of ScheduledThreadPoolExecutor to give an idea, perhaps overriding the implementation of ScheduledFuture<?> schedule( Runnable command, long delay, TimeUnit unit ) and figuring out to avoid the scheduling, only running the tasks once.

我想你可以使用ScheduledExecutor来实现这一点,方法是添加一个机制来将排队的任务替换为重新排队的任务。您可以查看ScheduledThreadPoolExecutor的源代码以提供一个想法,可能会覆盖ScheduledFuture schedule(Runnable命令,长延迟,TimeUnit单元)的实现并找出以避免调度,只运行一次任务。

Cheers,