Java.util.concurrent.ExecutorService interface with Examples
The ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. It is defined in java.util.concurrent package. It defines methods that execute the threads that return results, a set of threads and that determine the shutdown status.
The ExecutorService interface is implemented in a utility class called Executors. It defines methods that provide an implementation of the ExecutorService interface and many other interfaces, with some default settings.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
java.util.concurrent ↳ Interface ExecutorService
AbstractExecutorService ForkJoinPool ScheduledThreadPoolExecutor ThreadPoolExecutor
Methods in Executor interface:
1. shutdown()– When invoked the function causes all the currently executing tasks to terminate after completion in the order in which they were started and rejects any new incoming tasks.
2. shutdownNow()– On invocation, the function forcefully terminates all the tasks, regardless of their current state i.e running, waiting or ready. The lists of tasks that were in ready state in returned.
3. isShutdown()– The function tells whether the invoking executor is shutdown or not. Returns true if shutdown, otherwise returns false.
4. isTerminated()– The function checks if all the tasks have been completed post shutdown. Return true if completed, otherwise returns false.
5. awaitTermination()– The function waits for all the tasks to complete their execution after a shutdown request is found. It waits for the time specified by the timelimit argument.
boolean awaitTermination(long timelimit, TimeUnit unit) throws InterruptedException
6. submit()– The function adds a task that returns a result to the list of executing tasks for execution. It returns a Future object which returns the result of the task after completion.
1. <T> Future<T> submit(Callable<T> task)
2. <T> Future<T> submit(Runnable task, T result)
3. Future submit?(Runnable task)
7. invokeAll()– The function executes all the tasks contained in the collection. The list of Future objects is returned which contains the status and return values of the various tasks.
1. <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
2. <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
8. invokeAny()– The function executes all the tasks contained in the collection. On completion of any single task it returns its result and all the other tasks are canceled.
1. <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
2. <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
Example demonstrating an Executors
Starting A: 0 A: 1 A: 2 A: 3 A: 4 C: 0 C: 1 C: 2 C: 3 C: 4 D: 0 D: 1 D: 2 D: 3 D: 4 B: 0 B: 1 B: 2 B: 3 B: 4 Done