Detailed explanation of java thread pool

1. What is a thread pool: 
java.util.concurrent.Executors provides an implementation of the java.util.concurrent.Executor interface for creating a thread pool

. Multithreading technology mainly solves the problem of executing multiple threads in a processor unit. It can Significantly reduces the idle time of the processor unit and increases the throughput of the processor unit. Suppose the time required for a server to complete a task is: T1 creates the thread, T2 executes the task in the thread, and T3 destroys the thread. If: T1 + T3 is much larger than T2, thread pools can be used to improve server performance.

A thread pool includes the following four basic components:
     a. Thread pool manager (ThreadPool): used to create and manage thread pools, including creating thread pools, destroying thread pools, and adding new tasks;
     b. Worker threads (PoolWorker): Threads in the thread pool are in a waiting state when there are no tasks, and can execute tasks cyclically;
     c. Task interface (Task): The interface that each task must implement for the execution of tasks scheduled by worker threads. The entry, the finishing work after the task is executed, the execution status of the task, etc.;
     d. Task Queue (taskQueue): used to store unprocessed tasks. Provides a buffer mechanism.

   The thread pool technology focuses on how to shorten or adjust the T1 and T3 time, thereby improving the performance of the server program. It arranges T1 and T3 in the start and end time periods of the server program or some idle time periods respectively, so that when the server program processes client requests, there will be no overhead of T1 and T3.
    The thread pool not only adjusts the time period when T1 and T3 are generated, but it also significantly reduces the number of threads created, see an example:
    Suppose a server handles 50,000 requests a day, and each request needs a separate thread to complete. In the thread pool, the number of threads is generally fixed, so the total number of generated threads will not exceed the number of threads in the thread pool, and if the server does not use the thread pool to process these requests, the total number of threads is 50,000. The general thread pool size is much less than 50,000. Therefore, the server program using the thread pool will not waste time in processing requests in order to create 50000, thereby improving efficiency.

2. Common thread pools
①newSingleThreadExecutor
A thread pool of a single thread, that is, only one thread works in the thread pool at a time, and a single thread executes tasks
serially ②newFixedThreadExecutor(n)
A fixed number of thread pools, if a task is not submitted, it is a thread until the thread is reached The maximum number of pools, and then enter the waiting queue until the previous tasks are completed before continuing to execute
. ③newCacheThreadExecutor (recommended)
can cache the thread pool. When the size of the thread pool exceeds the threads required to process the task, it will reclaim some idle (usually 60 seconds without execution) thread, when there is a task, it will intelligently add a new thread to execute.
④newScheduleThreadExecutor has
an unlimited thread pool, which supports timing and periodic execution threads


  . The thread pool provided by java is more powerful. I believe that if you understand the working principle of the thread pool, you will not feel unfamiliar with the thread pool in the class library.

3.java thread pool instance

The java.util.concurrent package provides the implementation of the thread pool of threads.

The Executor interface represents a thread pool, and its execute (Runnable task) method is used to execute Runnable type tasks. Word interface of ExecutorService;
ExecutorService declares some methods for managing thread pools, such as the shutdown() method for closing thread pools;
Executors contains some static methods, which are responsible for generating various types of thread pool ExecutorService instances.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326251025&siteId=291194637