Home >Java >javaTutorial >Overview of Executor Service in Java
Executor and ExecutorService API is a crucial tool for managing and controlling thread execution. They are part of java.util.concurrent package. They simplifies the process of concurrent programming by abstracting the complexities of thread creation, management, and synchronization.
Executors is a utility class from java.util.concurrent package provides factory methods for creating and managing different types of ExecutorService instances. It simplifies the process of creating thread pools and allows us to easily create and manage executor instances with different configurations.
Executor API It is an interface available since Java 1.5. It provides execute(Runnable command) method. This is base interface and ExecutorService extends this interface. The given command will be executed in future time by new thread or thread from thread pool or same thread and doesn't returns void.
ExecutorService API It is an interface available since Java 1.5. It provides multiple methods to control the execution of tasks in concurrent programming. It support both Runnable and Callable tasks. It returns Future for task status. Below are the most frequent used methods.
submit() accepts a Callable or a Runnable task and returns Future type result.
invokeAny() accepts a collection of tasks to run, and returns the result of a successful execution of any one task.
invokeAll() accepts a collection of tasks to run, and returns the result of all tasks in the form of a list of Future objects type.
shutdown() it doesn't stop executor service immediately but doesn't accept new tasks. Once all current running tasks finished, it shutdown the executor service.
shutdownNow() it tries to stop the executor service immediately, but it doesn't guarantee that all the running tasks will be stopped at the same time.
awaitTermination(long timeout, TimeUnit unit) blocks/waits until all tasks are completed or timeout occurs or current thread is interrupted, whichever happens first. The current thread will be blocked.
Types of ExecutorService
ExecutorService fixedThreadPool = Executors.newScheduledThreadPool(5); Future<String> submit = fixedThreadPool.submit(() -> { System.out.println("Task executed by " + Thread.currentThread().getName()); return Thread.currentThread().getName(); }); fixedThreadPool.shutdown();
ExecutorService fixedThreadPool = Executors.newCachedThreadPool(); Future<String> submit = fixedThreadPool.submit(() -> { System.out.println("Task executed by " + Thread.currentThread().getName()); return Thread.currentThread().getName(); }); fixedThreadPool.shutdown();
ExecutorService fixedThreadPool = Executors.newSingleThreadExecutor(); Future<String> submit = fixedThreadPool.submit(() -> { System.out.println("Task executed by " + Thread.currentThread().getName()); return Thread.currentThread().getName(); }); fixedThreadPool.shutdown()
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // Single-threaded scheduler ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5); // Multi-threaded scheduler
scheduler.schedule(task, 10, TimeUnit.SECONDS); // Schedule task to run after 10 seconds. scheduler.scheduleAtFixedRate(task, 5, 10, TimeUnit.SECONDS); //It schedules a task to run every 10 seconds with an initial delay of 5 seconds. scheduler.scheduleWithFixedDelay(task, 5, 10, TimeUnit.SECONDS); //It schedules a task to run with a fixed delay of 10 seconds between the end of one execution and the start of the next, with an initial delay of 5 seconds. scheduler.schedule(() -> scheduler.shutdown(), 20, TimeUnit.SECONDS); //It schedules a shutdown of the scheduler after 20 seconds to stop the example.
Submitting Tasks to ExecutorService
Tasks can be submitted to the ExecutorService using the execute() and submit() methods. The execute() method is used for Runnable tasks, while submit() can handle both Runnable and Callable tasks."
executor.execute(new RunnableTask()); //fire-and-forgot executor.submit(new CallableTask()); //returns the status of task
Shutting Down ExecutorService
It's important to shut down the ExecutorService to release resources. You can do this using the shutdown() and shutdownNow() methods.
executor.shutdown(); // Initiates an orderly shutdown" executor.shutdownNow(); // Attempts to stop all actively executing tasks. executor.awaitTermination(long timeout, TimeUnit unit); //blocks the thread until all tasks are completed or timeout occurs or current thread is interrupted, whichever happens first. Returns `true `is tasks completed, otherwise `false`.
Recommended approach to shutdown
executor.shutdown(); try { // Wait for tasks to complete or timeout if (!executor.awaitTermination(120, TimeUnit.SECONDS)) { // If the timeout occurs, force shutdown executor.shutdownNow(); } } catch (InterruptedException ex) { executor.shutdownNow(); Thread.currentThread().interrupt(); }
About Runnable
About Callable
About Future
Happy Coding and Learning !!!
Please drop a comment if you have any question.
The above is the detailed content of Overview of Executor Service in Java. For more information, please follow other related articles on the PHP Chinese website!