I am attempting to executing a email job for every minute. Job has 5 services. Each 5 services should run in parallel.
Using ExecutorService :
ExecutorService service = null;
if (serviceMap != null && serviceMap.size() > 0) {
for (;;) {
service = Executors.newFixedThreadPool(serviceMap.size()); // Here Service Map size will be 5
for (Map.Entry entry : serviceMap.entrySet()) {
service.submit(new EmailService(conn, mailParam));
}
service.shutdown();
service.awaitTermination(1, TimeUnit.MINUTES);
}
}
Using ScheduledExecutorService :
ScheduledExecutorService scheduledExecutorService = null;
if (serviceMap != null && serviceMap.size() > 0) {
scheduledExecutorService = Executors.newFixedThreadPool(serviceMap.size()); // Here Service Map size will be 5
for (Map.Entry entry : serviceMap.entrySet()) {
ScheduledFuture scheduledFuture =
scheduledExecutorService.schedule(new EmailService(conn, mailParam),
60,
TimeUnit.SECONDS);
System.out.println("result = " + scheduledFuture.get());
}
scheduledExecutorService.shutdown();
}
If I use ExecutorService, for every minute I shutdown the service and execute the service again. Is it all right?
If I use ScheduledExecutorService, I couldn't be able to write code to execute service in parallel and couldn't be able to run the job for every minute.
Above is my code snippet. Please help me to resolve it.