ThreadPoolExecutor thread pool source notes

1. The purpose of using the thread pool:

Similar to the database connection pool, it prevents the system overhead caused by the frequent creation and destruction of objects (GC recycling and object initialization are both time-consuming operations)

2. The way to create a thread pool (the direct new way is not officially recommended, but the static way tool class provided by the Executors class is created) :

There are five kinds:

    A)Executors.newCachedThreadPool();

    B)Executors.newSingleThreadExecutor();

    C)Executors.newSingleThreadScheduledExecutor();

    D)Executors.newScheduledThreadPool();

    E)Executors.newFixedThreadPool();

    So what are the characteristics of these five methods?

        First of all, let's introduce the meaning of the thread pool initialization constructor and all its parameters:

        

    The above is the constructor of the thread pool. Here is an explanation of the specific meaning of the parameters:

        corePoolSize : The core thread, which is equivalent to the internal members of the company's developers, handles specific requests, and the internal employees are outsourced personnel (internal personnel + outsourced personnel <= total number of jobs)

        maximumPoolSize : the maximum number of threads, equivalent to the number of workstations in the company's development site,

        keepAliveTime : idle time, which is equivalent to how long after the demand is completed, the outsourcer needs to be dispatched back to the original company.

        workQueue : blocking queue, equivalent to demand

        handler : If the number of requests is greater than the maximum number of threads, the processing interceptor after the blocking queue is full; it is equivalent to too many demands, and it is very difficult for internal personnel and outsourcers to deal with them, and there is no time to deal with new demands. the way these needs

After understanding these parameters, let's return to the above question "So what are the characteristics of these five methods?"

2.1 newCachedThreadPool

    Source code:

    Features:

        A) As can be seen from the figure, the number of core threads is 0, and the number of out-of-core threads is the maximum value of Integer. This situation will improve the performance of programs that perform many short-term asynchronous tasks. A pool with long enough idle time will not consume any resource;

        B) SynchronousQueue is used, which is more commonly used in the thread pool of this queue. It is characterized by not storing any data, but only in the form of forwarding, similar to a transfer station

2.2 newSingleThreadExecutor

    Source code:

Description: FinalizableDelegatedExecutorService is a subclass of Executors, and the final execution is still the ThreadPoolExecutor class

    Features:

        A) There is only one core thread (but note that if this single thread fails to execute before shutdown, a new thread is required for subsequent tasks.)

        B) The queue has no boundaries, all requests are executed in order, and no task is active at any given time

2.3 newSingleThreadScheduledExecutor

    Source code:

    Features:

        A) Like newSingleThreadExecutor, there is only one core thread, but outsourced threads are almost unlimited, and outsourced threads are not allowed

        B) This thread pool can schedule commands to run after a given delay, or execute commands periodically, but note that if a single thread fails during execution before shutting down, a new thread will take its place if it needs to be executed.

2.4 newScheduledThreadPool

    Source code:

    Features:

        A) The number of core threads is set by the user, but there is almost no limit to outsourced threads, and outsourced threads are not allowed

        B) This thread pool can schedule commands to run after a given delay, or execute commands periodically.

2.5 newFixedThreadPool

    Source code:

    Features:

        A) No outsourced threads, the number of core threads and total threads are provided by the user

        B) The queue uses an unbounded queue

3. Execution process

    Source code:

    

    process:

        Drawing board (the picture of a thief who stole it):

è¿ ™ é ‡ Œå † ™ å ›¾ç ‰ ‡ æ è¿ °

        Character version:

    1. Get the number of threads currently working, if it is smaller than the core thread, add it directly to the core thread pool and execute it immediately, otherwise execute the second step

    2. Whether the queue is full, if not, add the task to the queue, if it is full, perform the third step

    3. Whether the outsourced thread is full, if not, create a thread to execute, otherwise, deal with the tasks that cannot be executed according to the strategy

    

Guess you like

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