An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks. An ExecutorService can be shut down, which will cause it to reject new tasks. Two different methods are provided for shutting down an ExecutorService. The shutdown method will allow previously submitted tasks to execute before terminating, while the shutdownNow method prevents waiting tasks from starting and attempts to stop currently executing tasks. Upon termination, an executor has no tasks actively executing, no tasks awaiting execution, and no new tasks can be submitted. An unused ExecutorService should be shut down to allow reclamation of its resources. Method submit extends base method Executor.execute(Runnable) by creating and returning a Future that can be used to cancel execution and/or wait for completion. Methods invokeAny and invokeAll perform the most commonly useful forms of bulk execution, executing a collection of tasks and then waiting for at least one, or all, to complete. (Class ExecutorCompletionService can be used to write customized variants of these methods.) The Executors class provides factory methods for the executor services provided in this package. Usage Examples Here is a sketch of a network service in which threads in a thread pool service incoming requests. It uses the preconfigured Executors.newFixedThreadPool factory method:
时间: 2024-04-14 18:27:21 浏览: 11
An ExecutorService is an Executor that provides additional methods for managing termination and tracking the progress of asynchronous tasks. It extends the base Executor interface and adds functionalities such as task submission, task cancellation, and waiting for task completion.
An ExecutorService can be shut down, which means it will stop accepting new tasks and initiate the process of terminating existing tasks. There are two methods provided for shutting down an ExecutorService:
1. `shutdown()`: This method allows previously submitted tasks to complete execution before terminating the ExecutorService. It will not interrupt already running tasks, but it will prevent new tasks from being accepted.
2. `shutdownNow()`: This method attempts to stop all actively executing tasks and prevents waiting tasks from starting. It interrupts the running tasks and may forcibly terminate them.
Once an ExecutorService is shut down, it reaches a terminated state where no tasks are actively executing, no tasks are awaiting execution, and no new tasks can be submitted. It is recommended to shut down an unused ExecutorService to release its resources.
The `submit()` method is an extended version of the base `execute(Runnable)` method in the Executor interface. It submits a task for execution and returns a Future object that can be used to track the progress of the task, cancel its execution, or wait for its completion.
The `invokeAny()` and `invokeAll()` methods are higher-level methods that execute a collection of tasks and wait for their completion. `invokeAny()` waits for at least one task to complete and returns the result of the first completed task. `invokeAll()` waits for all tasks to complete and returns a list of Future objects representing the results of all tasks.
The Executors class provides factory methods for creating different types of ExecutorService instances. For example, `Executors.newFixedThreadPool()` creates a thread pool with a fixed number of threads.
Here's an example usage of an ExecutorService with a fixed thread pool:
```java
ExecutorService executor = Executors.newFixedThreadPool(5);
// Submit tasks for execution
executor.submit(new Task1());
executor.submit(new Task2());
// Shut down the executor when no longer needed
executor.shutdown();
```
In this example, a fixed thread pool with 5 threads is created using the `newFixedThreadPool()` method. Two tasks, `Task1` and `Task2`, are submitted for execution using the `submit()` method. Finally, the executor is shut down using the `shutdown()` method.