Talk thread pool, to talk about the understanding of the ThreadPoolExecutor

Why use the thread pool, Advantages

Thread pool to do work is mainly to control the number of threads running , in the process into the task queue , and then start these tasks after thread creation , if the number of threads exceeds the maximum number exceeds the number of threads waiting in line , and other threads of execution is completed, and then taken from the queue to perform the task .

His main features are: thread multiplexing; maximum number of concurrent control; managing threads.

First: reduce resource consumption. By reusing the thread has been created to reduce thread creation and destruction caused by consumption.
Second: improving the response speed. When the mission arrives, the task may not need to wait until the thread creation can be implemented immediately.
Third: to improve the manageability of threads. A thread is a scarce resource, if the unlimited creation, not only consumes system resources, but also reduce the stability of the system, using a thread pool can be unified distribution, tuning and monitoring.

How to use the thread pool

Architecture Description

Java threads in the pool through the Executor implementation framework, the framework uses the Executor , The Executors , ExecutorService , ThreadPoolExecutor these categories.
Here Insert Picture Description

Coding

To understanding

Executors.newScheduledThreadPool ()
Java8 Newly: Executors.newWorkStealingPool (int parallelism), Java8 added, using currently available processors on the machine level parallelism as its

Emphasis

Ⅰ, Executors.newFixedThreadPool (int nThreads) apply: the implementation of long-term task, many good performance
Here Insert Picture Description
features are as follows:
1, create a fixed-length thread pool , you can control the maximum number of concurrent threads, excess threads will wait in the queue.
2, newFixedThreadPool thread pool created corePoolSize and maximumPoolSize values are equal, it uses LinkedBlockingQueue ;

Ⅱ, Executors.newSingleThreadExecutor () apply: the scene a task to perform a task of
Here Insert Picture Description
the main features are as follows:
1, create a single-threaded thread pool, use it only to perform the task only worker threads to ensure that all tasks are performed in a specified order .
2, newSingleThreadExecutor the corePoolSize and maximumPoolSize are set to 1, it uses a LinkedBlockingQueue .

Ⅲ、Executors.newCachedThreadPool() 适用:执行很多短期异步的小程序或者负载较轻的服务器
Here Insert Picture Description
主要特点如下:
1、创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
2、newCacheThreadPoolcorePoolSize 设置为0,将maximumPoolSize 设置为 Integer.MAX_VALUE,使用的 SynchronousQueue,也就是说来了任务就创建新线程运行,当线程空闲超过60秒,就销毁线程。

ThreadPoolExecutor

Here Insert Picture Description

线程池的几个重要参数介绍(7 大参数)

Here Insert Picture Description

  • 1、corePoolSize:线程池中的常驻核心线程数
    • 1、在创建了线程池后,当有请求任务来之后,就会安排池中的线程去执行请求任务,近似理解为今日当值线程

    • 2、当线程池中的线程数目达到 corePoolSize 后,就会把到达的任务放到缓存队列当中;

  • 2、maximumPoolSize:线程池能够容纳同时执行的最大线程数,此值必须大于等于1
  • 3、keepAliveTime:多余的空闲线程的存活时间。当前线程池数量超过 corePoolSize 时,当空闲时间达到 keepAliveTime 值时,多余空闲线程会被销毁直到只剩下 corePoolSize 个线程为止
    • 默认情况下:
      只有当线程池中的线程数大于 corePoolSize 时 keepAliveTime 才会起作用,直到线程池中的线程数不大于 corePoolSize。
  • 4、unit:keepAliveTime 的单位
  • 5、workQueue:任务队列,被提交但尚未被执行的任务。
  • 6、threadFactory:表示生成线程池中工作线程的线程工厂,用于创建线程一般用默认的即可。
  • 7、handler:拒绝策略,表示当队列满了并且工作线程大于等于线程池的最大线程数(maximumPoolSize)时如何来报道

线程池的底层工作原理

Here Insert Picture Description
以下重要:以下重要:以下重要:以下重要:以下重要:以下重要:

  • 1、在创建了线程池后,等待提交过来的任务请求。
  • 2、当调用 execute() 方法添加一个请求任务时,线程池会做如下判断:
    • 2.1、如果正在运行的线程数量小于 corePoolSize,那么马上创建线程运行整个任务;
    • 2.2, if the number of running threads is greater than or equal to corePoolSize , then this task in the queue ;
    • 2.3, the number of threads at this time if the queue is full and running is still less than maximumPoolSize , you still have to create a non-core thread to run this task immediately;
    • 2.4, if the queue is full and equal to or greater than the number of threads running maximumPoolSize , then the thread pool starts saturation refuse to execute strategy .
  • 3, when a thread is to complete the task, it is removed from a queue task is performed.
  • 4, when a thread is nothing more than a certain period of time (keepAliveTime), the thread pool will judge:
    if the number is greater than the currently running thread corePoolSize , then the thread is stopped.
    So after all the tasks of the thread pool complete it will eventually shrink to corePoolSize size .
Published 79 original articles · won praise 81 · views 7110

Guess you like

Origin blog.csdn.net/qq_35340189/article/details/104580058