Reference: creating a Java thread pool [1], Java threads: New features-thread pool [2], Java thread pool learning [3], introduction to thread pool threadpoolexecutor [4], explanation of thread pool instances in Java 5 [5], and usage and thinking of threadpoolexecutor [6]
[1] The bloggers themselves use threadgroup to implement a thread pool (which is easy to understand). The jdk1.4 version is used, and the jdk1.5 version and later versions provide a ready-made thread pool.
The Java. util. Concurrent. executors class API is introduced in [2.
[3] describes the class architecture of the thread pool in Java.
[4] instance applications with threadpoolexecutor.
[5] An instance with a thread pool is introduced to implement a port listening server based on the thread pool.★
[6] the source code is carefully analyzed, and the layout is good (^_^)
The top-level interface of the thread pool in Java is executor, but in a strict sense, executor is not a thread pool, but a tool for executing the thread. The real thread pool interface is executorservice. The following figure describes the class architecture of the thread pool.
Mark the important classes:
| Executorservice: |
Real thread pool interface. |
| Scheduledexecutorservice |
It can be similar to Timer/timertask to solve the problem of repeated task execution. |
| Threadpoolexecutor |
Executorservice default implementation. |
| Scheduledthreadpoolexecutor |
Inherits the implementation of the scheduledexecutorservice interface of threadpoolexecutor and class implementation of periodic task scheduling. |
It is complicated to configure a thread pool, especially when the thread pool principle is not clear, it is very likely that the thread pool configured is not optimal, therefore, some static factories are provided in the executors class to generate some common thread pools.
- Newsinglethreadexecutor: Create a single-threaded thread pool. This thread pool only has one thread working, which is equivalent to a single thread serial execution of all tasks. If this unique thread ends due to an exception, a new thread will replace it. This thread pool ensures that all tasks are executed in the order they are submitted.
- Newfixedthreadpool: Create a fixed thread pool. Each time a task is submitted, a thread is created until the thread reaches the maximum size of the thread pool. The size of the thread pool remains unchanged once it reaches the maximum value. If a thread ends due to an execution exception, the thread pool will add a new thread.
- Newcachedthreadpool: Create a cacheable thread pool. If the thread pool size exceeds the thread required to process the task, some Idle threads (not executed in 60 seconds) will be reclaimed. When the number of tasks increases, this thread pool can intelligently add new threads to process tasks. This thread pool does not limit the thread pool size. The thread pool size depends entirely on the maximum thread size that can be created by the operating system (or JVM.
- Newscheduledthreadpool: Creates an infinite thread pool. This thread pool supports regular and periodic task execution requirements.
- Newsinglethreadexecutor: Create a single-threaded thread pool. This thread pool supports regular and periodic task execution requirements.
My personal feeling is that new threadpoolexecutor (core_pool_size, max_pool_size, keepalive_time, time_unit, workqueue, rejectedexecutionhandler) provides more customized thread pool manufacturing methods. Because the newfixedthreadpool method is actually return New threadpoolexecutor.
Java. util. Concurrent. executors class APIs provide a large number of static methods for creating connection pools:
1. fixed thread pool size:
PackageBackstage;
ImportJava. util. Concurrent. executors;
ImportJava. util. Concurrent. executorservice;
Public ClassJavathreadpool {
Public Static VoidMain (string [] ARGs ){
// Create a thread pool with a fixed number of reusable threads
Executorservice pool = executors. newfixedthreadpool (2 );
// Create and implement the runnable interface object, and the thread object also implements the runnable interface.
Thread T1 =NewMythread ();
Thread t2 =NewMythread ();
Thread T3 =NewMythread ();
Thread t4 =NewMythread ();
Thread T5 =NewMythread ();
// Put the thread into the pool for execution
Pool.exe cute (T1 );
Pool.exe cute (T2 );
Pool.exe cute (T3 );
Pool.exe cute (T4 );
Pool.exe cute (T5 );
// Close the thread pool
Pool. Shutdown ();
}
}
ClassMythreadExtendsThread {
@ Override
Public VoidRun (){
System. Out. println (thread. currentthread (). getname () +"Executing...");
}
}
2. single task thread pool:
// create an executor that uses a single worker thread, run this thread in the unbounded queue mode.
executorservice pool = executors. newsinglethreadexecutor ();
the size of the two connection pools is fixed. When the thread (or task) of the pool to be added exceeds the maximum size of the pool, the thread pool needs to wait in queue. Once there are threads in the pool, a thread waiting in queue will be executed in the pool.
[2] also introduces: 3. variable-size thread pool newchahedthreadpool; 4. delay connection pool; 5. single task delay connection pool; 6. custom thread pool