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

查看:27
本文介绍了具有可变延迟的 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() 执行一次性,并在完成后再次安排(修改超时,如果必填).

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天全站免登陆