thread Pool2.1 Thread pool related class structure
Executor
Executorservice
Abstractexecutorservice
Threadpoolexecutor
2.2 Introduction to various classes
2.2.1 Interface Executor
Interface Executor is a simple standardized interface for defining custom subsystems that are similar to threads, including thread pools, asynchronous IO, and lightweight task frameworks. Depending on the specific Executor class used, the task may be executed in the newly created thread, in the existing task execution thread, or in the thread that calls execute (), and may be executed sequentially or concurrently.
The method that defines it:
void execute (Runnable command) executes the given command at some time in the future. The command may be executed in a new thread, a thread that is in the pool, or a thread that is being invoked, as determined by the Executor implementation.
2.2.2 Interface Executorservice
Executorservice provides a number of complete asynchronous task execution frameworks. Executorservice management tasks are queued and scheduled and allowed to be controlled off. Executorservice provides a way to schedule asynchronous execution, which executes any function represented by callable, and results in a similar Runnable. The future returns the result of a function that allows you to determine whether the execution is complete and provides a way to cancel execution.
The method that defines it:
boolean |
awaitTermination(long timeout, TimeUnit unit)
A request is closed, a timeout occurs, or when the front thread is interrupted, whichever occurs first, it will cause blocking until all tasks have completed execution. |
|
invokeall (collection<? extends Callable<t>> tasks)
Executes the given task, returning a list of future tasks that hold the status and results of the task when all tasks are completed. |
|
invokeall (collection<? extends Callable<t>> tasks, Long timeout, timeunit unit)
performs a given task, returning the future list of hold task status and results when all tasks are completed or when the timeout expires (whichever occurs first). |
|
invokeany (collection<? extends callable<t>>& Nbsp;tasks)
perform the given task, If a task has completed successfully (that is, an exception is not thrown), the result is returned. |
|
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
Executes the given task and returns the result if a task has completed successfully (that is, an exception is thrown) before the given time-out expires. |
boolean |
isShutdown()
Returns true if this executor is closed. |
boolean |
isTerminated()
Returns true if all tasks have completed after closing. |
void |
shutdown()
Starts a sequential shutdown, performs a previously submitted task, but does not accept new tasks. |
List<Run
nable> |
shutdownNow()
An attempt is made to stop all active tasks that are being performed, suspend processing of the awaited task, and return to the list of tasks awaiting execution. |
|
submit(Callable<T> task)
A task that submits a return value is used to execute, returning a future that represents the pending result of the task. |
Future<?> |
submit(Runnable task)
Submits a Runnable task for execution and returns a future that represents the task. |
|
submit(Runnable task, T result)
Submits a Runnable task for execution and returns a future that represents the task. |
2.2.3 Abstract classAbstractexecutorservice
The class provides the default implementation of Executorservice, which uses the implementation of the RunnableFuture submit, Invokeany, and InvokeAll methods returned by Newtaskfor, which RunnableFuture are the classes provided in this package by default FutureTask . For example, the implementation of Submit (Runnable) creates an associated runnablefuture class that will be executed and returned. Subclasses can override the Newtaskfor method to return runnablefuture implementations other than Futuretask.
2.2.4 Class Threadpoolexecutor
The threadpoolexecutor provides an adjustable, flexible thread pool. The Executors class provides factory methods for the common types and configurations of most Executor, as well as several utility methods that use them. Other Executor-based utilities include the concrete class Futuretask, which provides a common extensible implementation of the future, as well as executorcompletionservice, which helps coordinate the processing of asynchronous task groups.
It uses a number of possible pool threads to perform each submitted task, typically using a Executors factory method configuration. The thread pool resolves two different issues: because of the reduced overhead of each task invocation, they typically provide enhanced performance when performing a large number of asynchronous tasks, and can also provide methods for binding and managing resources, including the threads used to execute the task set. Each threadpoolexecutor also maintains some basic statistical data, such as the number of completed tasks.
For ease of use across a wide range of contexts, this class provides a number of adjustable parameters and extension hooks (hooks). However, it is strongly recommended that programmers use a more convenient Executors factory method Executors.newCachedThreadPool() (no thread pool, automatic thread recycling), Executors.newFixedThreadPool(int) (fixed-size thread pool), and Executors.newSingleThreadExecutor() (a single background thread), which are pre-defined settings for most usage scenarios.
Two queues
The Java.util.concurrent Concurrentlinkedqueue class provides an efficient, scalable, thread-safe, non-blocking FIFO queue. The five implementations in Java.util.concurrent support the extended Blockingqueue interface, which defines the blocking versions of Put and take: Linkedblockingqueue, Arrayblockingqueue, Synchronousqueue, Priorityblockingqueue and Delayqueue. These different classes cover the most common use contexts for producer-consumer, messaging, parallel task execution, and associated concurrency design.
Java.until.concurrent (i)