In order to reduce system resource consumption, improve system response speed and facilitate the control of the number of concurrent threads, thread pool emerged. Many people who are new to Java do not understand the classification and application of Java thread pools, so I will give you a brief explanation.


What is a Java thread pool?

A thread pool is a form of multithreaded processing in which tasks are added to a queue and then automatically started after threads are created, each thread using a default stack size, running at a default priority, and in a multithreaded cell. If a thread is idle in managed code, the thread pool inserts another worker thread to keep all processors busy. If all thread pools are always busy, but the queue contains pending work, the number of helper threads in the thread pool will never exceed the maximum after a period of time. Threads that exceed the maximum can be queued, but they can’t start until other threads have finished.

What are Java thread pools?

NewCachedThreadPool creates a cacheable thread pool procedure

A thread pool with an indefinite number of threads, the maximum number of threads being integer.max_value. A cacheable thread pool with the flexibility to recycle idle threads if the pool length exceeds processing requirements, or create new threads if none are available. However, all idle threads in the thread pool have a timeout limit of 60 seconds. After 60 seconds, idle threads are reclaimed. Calling execute reuses previously constructed threads (if available). This type of thread pool is suitable for performing a large number of small tasks, and when the entire thread pool is idle, the threads in the thread pool will timeout and be stopped.


NewFixedThreadPool Creates a fixed-length thread pool

Create a pool with a specified number of worker threads. Each time a task is submitted, a worker thread is created. When threads are idle, they are not recycled unless the pool is closed. If the number of worker threads reaches the initial maximum of the thread pool, the submitted task is deposited to the pool queue (with no size limit). Because newFixedThreadPool has only core threads and these core threads are not recycled, it is faster to respond to external requests.


NewScheduledThreadPool Creates a fixed-length thread pool

Create a thread pool that has a fixed number of core threads, an unlimited number of non-core threads, and is immediately reclaimed when non-core threads are idle. It can schedule commands to run after a given delay or execute them periodically. This type of thread pool is mainly used to perform scheduled tasks and repetitive tasks with fixed cycles.


NewSingleThreadExecutor creates a single threaded thread pool

This type of thread pool has only one core thread inside and executes in an unbounded queue, which eliminates the need to deal with thread synchronization between tasks. It ensures that all tasks are executed sequentially in the same thread and that no more than one thread can be active at any given time.


The top interface of a Java thread pool is Executor, but Executor is not strictly a thread pool, but a tool for executing threads. The real thread pool interface is the ExecutorService. There are several important classes in the Java thread pool:

1) The real thread pool interface of the ExecutorService.

2) ScheduledExecutorService can be similar to Timer/TimerTask to solve problems that require repeated execution of tasks.

3) Default implementation of ThreadPoolExecutor ExecutorService.

4) ScheduledThreadPoolExecutor inherit ThreadPoolExecutor ScheduledExecutorService interface implementations, periodic task scheduling of class implements.

Good programmer JavaEE distributed development

Baidu web disk link: extraction code: 7vQP