复习并发编程的基础知识之线程池

  • 并发编程中,线程池是很重要的一块内容。
    线程池是一种池化技术,线程池、字符串常量池和数据库链接池都属于池化技术。
    使用线程池的好处:
    1.提高了线程的利用率(想一想,我们不可能每打一个电话,就去买一部手机吧?)
    2.提高了程序的响应速度
    3.方便统一管理线程对象
    4.可以控制最大的并发数

线程池的模型如下: 复习并发编程的基础知识之线程池_第1张图片
其实和在银行办理业务是一样的
复习并发编程的基础知识之线程池_第2张图片
对应关系如下:
顾客–》任务
线程池–》银行
线程池中的线程对象–》银行的服务窗口
队列–》座位

在java中我们在使用线程池的时候,

ExecutorService threadPoolExecutor = new ThreadPoolExecutor(3,
                  5, 
                  1L, 
                  TimeUnit.SECONDS, 
                  new ArrayBlockingQueue<>(3)
                , Executors.defaultThreadFactory(), 
                new ThreadPoolExecutor.AbortPolicy());

对应API

 /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @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.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize} * @throws NullPointerException if {@code workQueue} * or {@code threadFactory} or {@code handler} is null */
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.acc = System.getSecurityManager() == null ? null : AccessController.getContext(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }

其中线程池的几个重要参数是面试中经常被问的内容。
corePoolSize:核心线程数
maximumPoolSize:最大线程数
keepAliveTime:存活时间
unit:和keepAliveTime成对出现,存活时间的单位
workQueue:the queue to use for holding tasks before they are executed.这个队列用于存放任务,什么样的任务呢?在这些任务被执行之前的任务。也就是说,任务没执行的时候都放在这个阻塞队列中。
threadFactory:线程工厂。
handler:拒绝策略。
复习并发编程的基础知识之线程池_第3张图片
复习并发编程的基础知识之线程池_第4张图片
从上面的这个图会发现,其实线程池是非公平的,因为后面来的任务比先来的在那等着的任务先执行了。

以这个

new ThreadPoolExecutor(3,
                  5, 
                  1L, 
                  TimeUnit.SECONDS, 
                  new ArrayBlockingQueue<>(3) //相当于就三个座位
                , Executors.defaultThreadFactory(), 
                new ThreadPoolExecutor.AbortPolicy());

为例说明:

  1. 当顾客1来的时候,银行创建一个窗口1,那么顾客1发现有窗口1空闲,那么窗口1来为顾客1服务,
    当顾客2来的时候,银行又创建一个窗口2,那么顾客2发现有窗口2空闲,那么窗口2来为顾客2服务,
    当顾客3来的时候,银行又创建一个窗口3,那么顾客3发现有窗口3空闲,那么窗口3来为顾客3服务,
    此时核心窗口数(核心线程数)达到了最大值
    当顾客4来的时候,发现没有窗口,但是座位还有位置,那么坐在座位上等待,
    当顾客5来的时候,发现没有窗口,但是座位还有位置,那么坐在座位上等待,
    当顾客6来的时候,发现没有窗口,但是座位还有位置,那么坐在座位上等待,
    此时座位(阻塞队列)已满
    当顾客7来的时候,发现没有窗口,而且座位也没有位置,这个时候银行行长说:来吧!窗口4你也别歇着了,工作吧!这样窗口4启动,窗口4为顾客7服务,
    当顾客8来的时候,发现没有窗口,而且座位也没有位置,这个时候银行行长又说:来吧!窗口5你也别歇着了,工作吧!这样窗口5启动,窗口5为顾客8服务,
    此时最大窗口数(最大线程数)达到了最大值
    当顾客9来的时候,发现没有窗口,而且座位也没有位置,这个时候,行长对顾客说:我们这里窗口数已经达到了(maximumPoolSize最大线程数),而且座位(BlockingQueue
    workQueue)也满了,我们得使用拒绝策略了(RejectedExecutionHandler
    handler,这种策略是抛出异常Exception in thread “main”
    java.util.concurrent.RejectedExecutionException:…)
    过了一段时间后,
    最后,当没有顾客来的时候了,行长看
    不用那么多窗口了,那么和窗口4和5说,再过1(keepAliveTime)s(unit)的时间你们可以下班了。所以我们称keepAliveTime为存活时间。
package cn.tulingxueyuan.xiaoshanshan.base.threadpool;

import java.util.concurrent.*;

public class MyThreadPool {

    public static void main(String[] args) {
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(3,
                5, 1l, TimeUnit.SECONDS, new ArrayBlockingQueue<>(2)
                , Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        try{
            for(int i=0;i<8;i++){
                threadPoolExecutor.execute(()->{
                    System.out.println(Thread.currentThread().getName() + "正在运行。。。。");
                    try {
                        Thread.sleep((int)(Math.random()*100)+1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
        }finally {
            threadPoolExecutor.shutdown();
        }



    }
}

你可能感兴趣的:(线程池,线程池参数)