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?
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.