Reputation: 3427
I want to submit a batch of tasks at once and also execute them periodically. Using ExecutorService
object and invokeall
method it's possible to run tasks at once. But trying to use scheduleAtFixedRate
, it's not compatible:
executor.scheduleAtFixedRate(executor.invokeAll(callables), initialDelay, period, TimeUnit.SECONDS );
How can I execute a batch of tasks at once and periodically?
Upvotes: 0
Views: 409
Reputation: 10072
There is nothing like invokeall
, but there is nothing wrong in looping via your runnables as there is also nothing like "at once" in reality:
ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
for (int i = 0; i < 10; i++) {
pool.scheduleAtFixedRate(() -> {
// do some work
}, 0, 10, TimeUnit.SECONDS);
}
Or if you have a collection of Runnable
:
ScheduledExecutorService pool = Executors.newScheduledThreadPool(runnables.size());
runnables.forEach((r) -> pool.scheduleAtFixedRate(r, 0, 10, TimeUnit.SECONDS));
Upvotes: 1