第八章-ThreadPoolExecutor请谈谈你的理解

为什么用线程池,优势

image-20191107110758465

线程池如何使用?

架构说明

image-20191107110852809

image-20191107110928954

编码实现

了解

image-20191107111018336

重点

  1. Executor.newFixThreadPool(int)

    执行长期任务。

    image-20191107111141091

  2. Executor.newSingleThreadExecutor

    一个任务。

    image-20191107111203377

  3. Executor.newCachThreadPool()

    执行很多任务。

    image-20191107111232588

  4. 案例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    /**
    * @desc 线程池demo
    * @Author xw
    * @Date 2019/8/23
    */
    public class MyThreadPoolDemo {
    public static void main(String[] args) {
    // ExecutorService threadPool = Executors.newFixedThreadPool(5); // 固定线程数
    // ExecutorService threadPool = Executors.newSingleThreadExecutor(); // 单池
    ExecutorService threadPool = Executors.newCachedThreadPool();
    try {
    for (int i = 1; i <= 10; i++) {
    threadPool.execute(() -> {
    System.out.println(Thread.currentThread().getName() + "\t 办理业务");
    });
    }
    } finally {
    threadPool.shutdown();
    }
    }
    }

ThreadPoolExecutor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters and default thread factory and rejected execution handler.
* It may be more convenient to use one of the {@link Executors} factory
* methods instead of this general purpose constructor.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}

线程池的几个重要参数介绍?

  • corePoolSize:线程池中的常驻核心线程数
  • maxnumPoolSize:线程池能够容纳同时执行的最大线程数,此值必须大于等于1
  • keepAliveTime:多余的空闲线程的存活时间。当线程池数量超过corePoolSize时,当空闲时间达到keepAliveTime值时,多余空闲线程会被销毁直到只剩下corePoolSize个线程为止
  • unit:KeepAliveTime单位
  • workQueue:任务队列,被提交但尚未被执行的任务。
  • threadFactory:表示生成线程池中工作线程的线程工厂,用于创建线程一般默认的即可。
  • handler:拒绝策略,表示当队列满了并且工作线程大于等于线程池的最大线程数(maxnumPoolSize)

说说线程池的底层原理

image-20191107111559716