문제

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.

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top