Question

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.

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top