ScheduledExecutorService with variable delay

前端 未结 5 2054
别那么骄傲
别那么骄傲 2020-12-09 03:44

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

public void scheduleTask(int delay, TimeUnit ti         


        
5条回答
  •  -上瘾入骨i
    2020-12-09 04:18

    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:

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

提交回复
热议问题