具有可变延迟的ScheduledExecutorService

时间:2009-10-05 09:41:59

标签: java concurrency executorservice blockingqueue

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

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

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

  • 我们的想法是获取数据更新流并将它们批量传播到GUI
  • 用户应该能够改变更新频率

5 个答案:

答案 0 :(得分:26)

使用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);

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

的需要

答案 1 :(得分:7)

我认为你不能改变固定费率延迟。我认为您需要使用schedule()执行一次性操作,并在完成后再次安排(如果需要,可以修改超时)。

答案 2 :(得分:2)

如果您尝试以特定间隔处理多个队列任务,是否应该使用scheduleAtFixedRatescheduleWithFixedDelay只会等待指定的延迟,然后从队列中执行一个任务。

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

我最近不得不使用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);
}