[concurrency-interest] ScheduledThreadPoolExecutor woes

Ernst, Matthias matthias.ernst at coremedia.com
Sun Jan 21 04:20:18 EST 2007


> This is something I thought of too, but it is somewhat kludgy for a task 
> to have knowledge of the pool and to reschedule itself. Although this is 
> done in ScheduledThreadPoolExecutor.ScheduledFutureTask.runPeriodic() too:

>It would be great if there was a scheduleXXX() method which took an 
> Period parameter:

> |scheduleAtFixedRate(Runnable command, long initialDelay,  Period 
> period, TimeUnit unit)|

You could write it yourself along the lines of:

public static <R extends Runnable&Delayed> void schedule(ScheduledExecutorService ex, R command) {
  long delay = command.getDelay(TimeUnit.NANOS);
  if(delay > 0) {
    ex.schedule(new Runnable() {
      public void run() {
        command.run();
        long delay = command.getDelay(TimeUnit.NANOS);
        if(delay > 0)
          ex.schedule(this, delay, TimeUnit.NANOS);
      }
    }, delay, TimeUnit.NANOS);
  }
}

I've left cancellation open here (thus returns void), but it could be implemented. You could return a delegating ScheduledFuture that forwards to the result of the latest call to ex.schedule().

Matthias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20070121/1b6c03ba/attachment.html 


More information about the Concurrency-interest mailing list