并发编程7-Executor线程池原理

简介

进程、线程、协程

进程是应用程序的启动实例,进程拥有代码和打开的文件资源、数据资源、独立的内存空间。

线程从属于进程,是程序的实际执行者,一个进程至少包含一个主线程,也可以有更多的子线程,线程拥有自己的栈空间。

线程是调度cpu资源的最小单位

用户级线程,
协程(Coroutines)是一种比线程更加轻量级的存在,正如一个进程可以拥有多个线程一样,一个线程可以拥有多个协程。

线程池处理流程

线程池内部通过线程+队列实现,判断当前线程数 -》 核心线程【<创建核心线程; 】 -》工作队列【未满时加入队列,否则判断最大线程数】 -》最大线程数【<创建临时线程,>=拒绝】 -》拒绝策略

在这里插入图片描述

源码

/**
     * 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:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@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) {

参数解释:
corePoolSize: 要保留在池中的线程数,即使它们处于空闲状态,除非设置了 {@code allowCoreThreadTimeOut}
maximumPoolSize: 池中允许的最大线程数
keepAliveTime: 当线程数大于核心数时,多余的空闲线程等待新任务的最长时间,超过该时间则空闲线程终止。
unit: {@code keepAliveTime} 方法参数对应的时间单位
workQueue:存放等待执行的任务 的队列。此队列将仅包含 {@code execute} 方法提交的 {@code Runnable} 任务。
threadFactory:executor中创建新线程时使用的线程工厂。
handler:当线程达到上限并且队列满时,执行该策略处理新来的任务。

在这里插入图片描述

相关知识点

线程池中的一个线程异常了会被怎么处理?

1、execute方法,可以看异常输出在控制台,而submit在控制台没有直接输出,必须调用Future.get()方法时,可以捕获到异常。

2、一个线程出现异常不会影响线程池里面其他线程的正常执行。

3、线程不是被回收而是线程池把这个线程移除掉,同时创建一个新的线程放到线程池中。

核心线程是否回收

可以回收,线程池内部有allowCoreThreadTimeOut这么一个参数,有set方法可以设置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值