You can create ExecutorService
that contains only one thread to run your jobs:
ExecutorService executorService = Executors.newSingleThreadExecutor();
When you submit several jobs to this service using invokeAll
method it will run them sequentially using single Thread
instance.
If you want to use ScheduledExecutorService
to run your jobs every N minutes you can switch to
ScheduledExecutorService scheduledExecutorService =
Executors.newSingleThreadScheduledExecutor();
that will provide you with additional methods to better control your jobs.
As you can see invokeAll
method is derived from ExecutorService
, so it does not provide you scheduling options. Still this is just a shortcut and you can schedule multiple Runnable
instances using regular loop:
for (Runnable job : allJobs) {
scheduledExecutorService.scheduleAtFixedRate(job, 0L, N, TimeUnit.MINUTES);
}