The top interface of the thread pool in Java is executor, but strictly speaking, executor is not a thread pool, but a tool for executing threads. The real thread pool interface is executorservice.
The following diagram provides a complete picture of the class architecture of the thread pool.
First of all, the Execute method of executor only performs a runnable task, and of course the final implementation class is also initiated in the thread from a certain angle. Depending on the execution strategy of the thread pool, this task may be executed in a new thread, or in a thread pool, or even in the caller's thread (equivalent to running the runnable Run method directly). This will be explained in detail later.
Executorservice has added several methods based on executor, of which there are two core methods:
- Future<?> Submit (Runnable Task)
- <T> future<t> Submit (callable<t> Task)
Both methods submit tasks to the thread pool, and the difference is that runnable has no results after execution, and there is a result after the callable has finished executing. This is useful for passing states and results in multiple threads. In addition, their same point is that they return a future object. The future object can block the thread until it finishes running (get the results, if any), cancel the task execution, and, of course, detect whether the task is canceled or executed.
Before we have a future, we detect whether a thread is executing, usually using thread.join () or a dead loop plus a status bit to describe the execution of the thread. Now there is a better way to block threads, to detect tasks that have finished executing or even cancel tasks that have not been performed.
Scheduledexecutorservice describes the same functionality as Timer/timertask, and addresses issues that require repetitive execution of tasks. This includes the one-time delay execution, the periodic execution of the delay time, and the periodic execution of the fixed delay time. Of course, inheriting Executorservice's scheduledexecutorservice has all the features of Executorservice.
Threadpoolexecutor is the default implementation of Executorservice, where the configuration and policy are more complex, with detailed analysis in later chapters.
Scheduledthreadpoolexecutor is the implementation of the Scheduledexecutorservice interface that inherits Threadpoolexecutor, and the class implementation of periodic task scheduling, which will be analyzed in detail in the later chapters.
A little mention here is the Completionservice interface, which is a thread pool wrapper that describes the sequential fetch execution results. It relies on a specific thread pool schedule, but is able to get execution results based on the order in which the tasks are executed, which in some cases may increase concurrency efficiency.
To configure a thread pool is more complex, especially if the thread pool principle is not very clear, it is likely that the thread pool configured is not superior, so there are some static factories in the executors class that generate some common thread pools.
- Newsinglethreadexecutor: Creates a single thread pool. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted.
- Newfixedthreadpool: Creates a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. Once the maximum size of the thread pool is reached, the thread pool will be replenished with a new thread if it ends up executing an exception.
- Newcachedthreadpool: Creates a cacheable thread pool. If the size of the thread pool exceeds the thread required to process the task, then a partially idle (60 second non-performing task) thread is reclaimed, and when the number of tasks increases, the thread pool can intelligently add new threads to handle the task. This thread pool does not limit the size of the thread pool, and the thread pool size is entirely dependent on the maximum thread size that the operating system (or JVM) can create.
- Newscheduledthreadpool: Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks.
- Newsinglethreadscheduledexecutor: Creates a single thread pool. This thread pool supports the need to schedule and periodically perform tasks.
Executor and executors