'Do we need to call awaitTermination on ScheduledExecutorService?

I am new to Java multithreading. I have thousands of scheduled tasks/threads that I need to be executed. I am using the following code:

ScheduledExecutorSerivce scheduleService = Executors.newScheduledThreadPool(90);
Map<Interger,Interger> loginMap = new HashMap<>();//contain login time of scheduled threads
for(int i = 0; i < taskCount ; i++) {
  scheduleService.schedule(new MyCallableWorker(),loginMap.get(i),TimeUnit.SECONDS)
}
scheduleService.shutdown();
//do I need to call the following because I dont know any timeout value or above shutDown() is enough
while(!scheduleService.isTerminated()) {
}

Also please let me know what should be the ideal count for thread pool. I have set as 90 but I want pool which can grow as needed but looks like ScheduleExecutorService has no such API.



Solution 1:[1]

You need to call awaitTermination() if your application is supposed to do some action after it makes sure that all current tasks are finished. If your application does not care when the current tasks finish then you don't need to awaitTermination().

About growing thread pool size, you can directly use ScheduledThreadPoolExecutor.

/**
 * Creates a new {@code ScheduledThreadPoolExecutor} with the
 * given core pool size.
 *
 * @param corePoolSize the number of threads to keep in the pool, even
 *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
 * @throws IllegalArgumentException if {@code corePoolSize < 0}
 */
public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
          new DelayedWorkQueue());
}

Keep the core pool size as what you need at minimum and let it grow till the max pool size.

Solution 2:[2]

The scheduleService.shutdown();

will indicate that the the scheduler that it should not take new tasks and the existing one will either be cancelled or they may be executed (but not repeated) depending on the policy used:

"If the ExecuteExistingDelayedTasksAfterShutdownPolicy has been set false, existing delayed tasks whose delays have not yet elapsed are cancelled. And unless the ContinueExistingPeriodicTasksAfterShutdownPolicy has been set true, future executions of existing periodic tasks will be cancelled."

Now in your code you should call:

scheduleSerice.awaitTermination()

instead of while(!scheduleService.isTerminated()). The while loop will burn all the cycles of the processor as it will be constantly checking the executor state.

If you don't call the awaitTermination method (or your loop aproach), the method from which you called your shceduleService.schedule will simply finish (quit), and potentially your whole program will terminate (without completing the jobs).

But it depends on your design. If it is asynrchonous, than, you can simply ignore the awaittermination and continue your work. The jobs will be processed in the background.

Your thread pool should not growth!!!! It should match your avaialbe resources == CPUs. For not IO tasks that are waiting for resources it should be around processor counts, for IO tasks double the number of CPUs, anything more makes no sense, it will actually slow the execution down as there will thread throtling.

The pool size limits how many tasks will be executed simultaneusly, not the size of task queue, which unless set otherwise is unbounded and grows.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hemant1900
Solution 2