ScheduledExecutorService具有可变延迟 [英] ScheduledExecutorService with variable delay

查看:215
本文介绍了ScheduledExecutorService具有可变延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个任务从java.util.concurrent.BlockingQueue中拉取元素并处理它们。

Suppose I have a task that is pulling elements from a java.util.concurrent.BlockingQueue and processing them.

public void scheduleTask(int delay, TimeUnit timeUnit)
{
    scheduledExecutorService.scheduleWithFixedDelay(new Task(queue), 0, delay, timeUnit);
}

如果可以动态更改频率,如何安排/重新安排任务?

How can I schedule / reschedule the task if the frequency can be changed dynamically?


  • 这个想法是采取数据更新流并将它们批量传播到GUI

  • 用户应该能够更改更新的频率

推荐答案

您可以更改固定速率延迟。我认为您需要使用 schedule()来执行一次性操作,并且在完成后再次计划一次(具有修改的超时if必需)。

I don't think you can change a fixed rate delay. I think you need to use schedule() to perform a one-shot, and schedule again once that has completed (with a modified time out if required).

这篇关于ScheduledExecutorService具有可变延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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