Reputation: 39951
I have a singleThreadExecutor that I'm feeding a Runnable with a scheduledFixedDelay like this
Runnable periodic = new Runnable() { ... }
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(periodic, 1, 1, TimeUnit.MINUTES);
It will run with 1 minute delay between executions. Problem is, sometimes I need to run it "on demand". Is that possible?
I've thought about cancelling the execution, run the Runnable and restart the execution but what I would really like is some simple method thats just executes the Runnable a head of time and the reschedules it to run again in one minute.
Upvotes: 7
Views: 1677
Reputation: 533442
When you want the task to run you can submit it
executor.submit(periodic);
or add it with a delay
executor.schedule(periodic, delay, units);
Upvotes: 4