李锋镝的博客

  • 首页
  • 时间轴
  • 留言
  • 插件
  • 左邻右舍
  • 关于我
    • 关于我
    • 另一个网站
  • 知识库
  • 赞助
Destiny
自是人生长恨水长东
  1. 首页
  2. 转载
  3. 技术
  4. 正文

ThreadPoolExecutor如何实现线程复用及超时销毁

2021年6月18日 16722点热度 3人点赞 0条评论

以下,基于JDK1.8。

1. 线程复用

我们知道Thread.start执行之后,线程就不能再次执行了,那ThreadPoolExecutor是如何做到线程复用的呢?
原理很简单,在实际执行的线程外部套一个Thread,外层Thread的run方法while循环执行实际执行线程的run方法,实现线程的复用并且执行之后不销毁。下面是伪代码:

// 任务等待队列
BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue();

new Thread(() -> {
    for (;;){
        Runnable runnable = taskQueue.poll();//队列里拿
        runnable.run();//同步执行
    }
}).start();// 异步while执行

下面是ThreadPoolExecutor的重点代码:

final void runWorker(Worker w) {
    Thread wt = Thread.currentThread();
    Runnable task = w.firstTask;
    w.firstTask = null;
    w.unlock(); // allow interrupts
    boolean completedAbruptly = true;
    try {
        while (task != null || (task = getTask()) != null) { // 阻塞获取等待任务
            w.lock();
            if ((runStateAtLeast(ctl.get(), STOP) ||
                 (Thread.interrupted() &&
                  runStateAtLeast(ctl.get(), STOP))) &&
                !wt.isInterrupted())
                wt.interrupt();
            try {
                beforeExecute(wt, task);
                Throwable thrown = null;
                try {
                    task.run(); // 实际阻塞执行
                } catch (RuntimeException x) {
                    thrown = x; throw x;
                } catch (Error x) {
                    thrown = x; throw x;
                } catch (Throwable x) {
                    thrown = x; throw new Error(x);
                } finally {
                    afterExecute(task, thrown);
                }
            } finally {
                task = null;
                w.completedTasks++;
                w.unlock();
            }
        }
        completedAbruptly = false;
    } finally {
        processWorkerExit(w, completedAbruptly);
    }
}

2. 线程销毁

我们知道,在创建线程池的时候有超时参数keepAliveTime,那么线程池是如何实现精确的超时销毁呢?

这个是结合BlockingQueue的阻塞超时来实现的,下面是源码:

 /**
 * ...
 * @return task, or null if the worker must exit, in which case workerCount is decremented
 * 翻译: 返回task,如果worker必须退出,则返回null,在这种情况下workerCount递减
 */
private Runnable getTask() {
    boolean timedOut = false; // Did the last poll() time out?

    for (;;) {
        int c = ctl.get();
        int rs = runStateOf(c);

        // Check if queue empty only if necessary.
        if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
            decrementWorkerCount();
            return null;
        }

        int wc = workerCountOf(c);

        // Are workers subject to culling?
        boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

        if ((wc > maximumPoolSize || (timed && timedOut))
            && (wc > 1 || workQueue.isEmpty())) {
            if (compareAndDecrementWorkerCount(c))
                return null;
            continue;
        }

        try {
            Runnable r = timed ?
                workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : // 重点在这,如果超时没有获取到任务,则返回null,销毁线程。
                workQueue.take();
            if (r != null)
                return r;
            timedOut = true;
        } catch (InterruptedException retry) {
            timedOut = false;
        }
    }
}

3. 为什么用BlockingQueue

  1. 获取等待任务的时候,直接用阻塞代替通知轮询,提高性能,减少代码复杂度。
  2. 复用阻塞超时获取等待任务实现线程超时销毁,设计精巧。
  3. 本身就是支持并发操作的,不用额外维护线程安全。
除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/archives/transport/3456

相关文章

  • ThreadLocal如何解决内存泄漏问题
  • Java并发编程之如何保证线程顺序执行
  • TransmittableThreadLocal介绍与使用
  • SpringBoot定时任务 - 经典定时任务设计:时间轮(Timing Wheel)案例和原理
  • @Resource 和 @Autowired 的区别
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: JAVA 多线程 线程
最后更新:2021年6月18日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >

文章评论

1 2 3 4 5 6 7 8 9 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 55 57 58 60 61 62 63 64 65 66 67 69 72 74 76 77 78 79 80 81 82 85 86 87 90 92 93 94 95 96 97 98 99
取消回复

遥知兄弟登高处,遍插茱萸少一人。

最新 热点 随机
最新 热点 随机
SpringBoot框架自动配置之spring.factories和AutoConfiguration.imports 应用型负载均衡(ALB)和网络型负载均衡(NLB)区别 什么是Helm? TransmittableThreadLocal介绍与使用 ReentrantLock深度解析 RedisTemplate和Redisson的区别
玩博客的人是不是越来越少了?准备入手个亚太的ECS,友友们有什么建议吗?什么是Helm?2024年11月1号 农历十月初一别再背线程池的七大参数了,现在面试官都这么问URL地址末尾加不加“/”有什么区别
MySQL深度分页 如何查看JVM参数 Xshell 家庭、学校免费版下载 关于8月29号下午博客故障的一些记录 如何提升微服务的幸福感 多层嵌套map对象转扁平化map
标签聚合
K8s ElasticSearch SQL docker 日常 Redis 设计模式 架构 分布式 文学 多线程 JVM MySQL 面试 SpringBoot 数据库 IDEA JAVA 教程 Spring
友情链接
  • i架构
  • 临窗旋墨
  • 博友圈
  • 博客录
  • 博客星球
  • 哥斯拉
  • 志文工作室
  • 搬砖日记
  • 旋律的博客
  • 旧时繁华
  • 林羽凡
  • 知向前端
  • 蜗牛工作室
  • 集博栈
  • 韩小韩博客
  • 風の声音

COPYRIGHT © 2025 lifengdi.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Dylan

津ICP备2024022503号-3