java 具有可变延迟的 ScheduledExecutorService

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1519091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:53:19  来源:igfitidea点击:

ScheduledExecutorService with variable delay

javaconcurrencyexecutorserviceblockingqueue

提问by parkr

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

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

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?

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

  • The idea is to take a stream of data updates and propagate them in batch to a GUI
  • The user should be able to vary the frequency of updates
  • 这个想法是获取数据更新流并将它们批量传播到 GUI
  • 用户应该能够改变更新的频率

采纳答案by Brian Agnew

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).

我认为您无法更改固定速率延迟。我认为您需要使用schedule()来执行一次性,并在完成后再次安排(如果需要,修改超时)。

回答by Adamski

Use schedule(Callable<V>, long, TimeUnit)rather than scheduleAtFixedRateor scheduleWithFixedDelay. Then ensure that your Callable reschedules itself or a new Callable instanceat some point in the future. For example:

使用schedule(Callable<V>, long, TimeUnit)而不是scheduleAtFixedRatescheduleWithFixedDelay。然后确保您的 Callable在未来的某个时刻重新安排自身或新的 Callable 实例。例如:

// Create Callable instance to schedule.
Callable<Void> c = new Callable<Void>() {
  public Void call() {
   try { 
     // Do work.
   } finally {
     // Reschedule in new Callable, typically with a delay based on the result
     // of this Callable.  In this example the Callable is stateless so we
     // simply reschedule passing a reference to this.
     service.schedule(this, 5000L, TimeUnit.MILLISECONDS);
   }  
   return null;
  }
}

service.schedule(c);

This approach avoids the need to shut down and recreate the ScheduledExecutorService.

这种方法避免了关闭和重新创建ScheduledExecutorService.

回答by jarnbjo

Shouldn't you be using scheduleAtFixedRateif you are trying to process several queue tasks with a specific interval? scheduleWithFixedDelaywill only wait for the specified delay and then execute one task from the queue.

scheduleAtFixedRate如果您尝试以特定时间间隔处理多个队列任务,您不应该使用吗?scheduleWithFixedDelay只会等待指定的延迟,然后从队列中执行一项任务。

In either case, the schedule*methods in a ScheduledExecutorServicewill return a ScheduledFuturereference. If you want to change the rate, you can cancel the ScheduledFutureand reschedule the task with a different rate.

在任何一种情况下,a 中的schedule*方法都ScheduledExecutorService将返回一个ScheduledFuture引用。如果您想更改费率,您可以取消 ScheduledFuture并以不同的费率重新安排任务。

回答by sfussenegger

scheduleWithFixedDelay(...) returns a RunnableScheduledFuture. In order to reschedule it, you might just cancel and reschedule it. To reschedule it, you may just wrap the RunnableScheduledFuture wit a new Runnable:

scheduleWithFixedDelay(...) 返回一个 RunnableScheduledFuture。为了重新安排它,您可能只是取消并重新安排它。要重新安排它,您可以将 RunnableScheduledFuture 包装成一个新的 Runnable:

new Runnable() {
    public void run() {
        ((RunnableScheduledFuture)future).run();
    }
};

回答by Chibueze Opata

I had to do this recently using ScheduledFuture and didn't want to wrap Runnable or such. Here's how I did it:

我最近不得不使用 ScheduledFuture 来做这件事,不想包装 Runnable 之类的东西。这是我如何做到的:

private ScheduledExecutorService scheduleExecutor;
private ScheduledFuture<?> scheduleManager;
private Runnable timeTask;

public void changeScheduleTime(int timeSeconds){
    //change to hourly update
    if (scheduleManager!= null)
    {
        scheduleManager.cancel(true);
    }
    scheduleManager = scheduleExecutor.scheduleAtFixedRate(timeTask, timeSeconds, timeSeconds, TimeUnit.SECONDS);
}

public void someInitMethod() {

    scheduleExecutor = Executors.newScheduledThreadPool(1);    
    timeTask = new Runnable() {
        public void run() {
            //task code here
            //then check if we need to update task time
            if(checkBoxHour.isChecked()){
                changeScheduleTime(3600);
            }
        }
    };

    //instantiate with default time
    scheduleManager = scheduleExecutor.scheduleAtFixedRate(timeTask, 60, 60, TimeUnit.SECONDS);
}