Search code examples
javamultithreadinginterruptexecutorservice

should I call `executorService.shutdown` and `executorService.awaitTermination`?


/**
 * Blocks until all tasks have completed execution after a shutdown
 * request, or the timeout occurs, or the current thread is
 * interrupted, whichever happens first.
 *
 * @param timeout the maximum time to wait
 * @param unit the time unit of the timeout argument
 * @return {@code true} if this executor terminated and
 *         {@code false} if the timeout elapsed before termination
 * @throws InterruptedException if interrupted while waiting
 */
boolean awaitTermination(long timeout, TimeUnit unit)
    throws InterruptedException;

What does this completed execution after a shutdown mean?

Does it mean I have to execute a shutdown API ?

Should my code be like:

@Test
public void setUserNamePropertyFromMultiThreadsUpdatesCorrectly() throws Exception {
        ..

        List<Callable<Void>> callables = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            callables.add(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    ..
                    assertTrue(isSuccess);
                    return null;
                }
            });
        }
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        try {
            executorService.invokeAll(callables);
            executorService.shutdown();
            executorService.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

   ..
}

or

 try {
                executorService.invokeAll(callables);
                executorService.awaitTermination(1, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }

What does This method does not wait for previously submitted tasks to complete execution mean? sending stop interrupt even if the task is not completed?

/**
 * Initiates an orderly shutdown in which previously submitted
 * tasks are executed, but no new tasks will be accepted.
 * Invocation has no additional effect if already shut down.
 *
 * <p>This method does not wait for previously submitted tasks to
 * complete execution.  Use {@link #awaitTermination awaitTermination}
 * to do that.
 */
void shutdown();

Solution

  • shutdown : no more incoming tasks.

    awaitTermination : invoked after a shutdown request.

    What does this completed execution after a shutdown mean?

    You should call shutdown first. Otherwise, you might be waiting for a very long time, since awaitTermination doesn't actually shut down your executor.

    Does it mean I have to execute a shutdown API?

    Yes, As explained above

    What does This method does not wait for previously submitted tasks to complete execution mean?

    It means, shutdown is not a blocking call... method returns immediately after invocation. To be blocked till everything is complete.. you call awaitTermination.