Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force an extra run of a scheduled execution?

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.

like image 323
Andreas Wederbrand Avatar asked Oct 17 '12 13:10

Andreas Wederbrand


Video Answer


1 Answers

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);
like image 101
Peter Lawrey Avatar answered Oct 18 '22 04:10

Peter Lawrey