线程池为什么能维持线程不释放,随时运行各种任务?

2023-10-27

版权声明:本文为博主原创文章,未经博主允许不得转载。技术交流可邮:cjh94520@outlook.com https://blog.csdn.net/cjh94520/article/details/70545202

线程池

之前一直有这个疑问:我们平时使用线程都是各种new Thread(),然后直接在run()方法里面执行我们要做的各种操作,使用完后需要做什么管理吗?线程池为什么能维持住核心线程不释放,一直接收任务进行处理呢?

线程

线程无他,主要有两个方法,我们先看看start()方法介绍:

    /**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the <code>run</code> method of this thread.
     * <p>
     * The result is that two threads are running concurrently: the
     * current thread (which returns from the call to the
     * <code>start</code> method) and the other thread (which executes its
     * <code>run</code> method).
     * <p>
     * It is never legal to start a thread more than once.
     * In particular, a thread may not be restarted once it has completed
     * execution.
     *
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        #run()
     * @see        #stop()
     */

    public synchronized void start() {
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        started = false;
        try {
            nativeCreate(this, stackSize, daemon);
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }
  • 从这个方法解释上看,start()这个方法,最终会交给VM 去执行run()方法,所以一般情况下,我们在随便一个线程上执行start(),里面的run()操作都会交给VM 去执行。
  • 而且还说明,重复启用线程是不合法的,当一个线程完成的时候,may not be restarted once。

    那么这种情况下,线程池是怎么做的?他为什么就能够重复执行各种任务呢?


带着各种疑问,我们去看看线程池自己是怎么实现的。

线程池

线程池常用的创建方法有那么几种: 
1. newFixedThreadPool() 
2. newSingleThreadExecutor() 
3. newCachedThreadPool() 
4. newScheduledThreadPool()

这4个方法创建的线程池实例具体就不一一介绍,无非是创建线程的多少,以及回收等问题,因为其实这4个方法最后都会调用统一的构造方法:

    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

具体来说只是这几个值的不同决定了4个线程池的作用: 
1. corePoolSize 代表核心线程池的个数,当线程池当前的个数大于核心线程池的时候,线程池会回收多出来的线程 
2. maximumPoolSize 代表最大的线程池个数,当线程池需要执行的任务大于核心线程池的时候,会创建更多的线程,但是最大不能超过这个数 
3. keepAliveTime 代表空余的线程存活的时间,当多余的线程完成任务的时候,需要多长时间进行回收,时间单位是unit 去控制 
4. workQueue 非常重要,这个工作队列会存放所有待执行的Runnable对象

 @param workQueue the queue to use for holding tasks before they areexecuted. 
 This queue will hold only the {@code Runnable} tasks submitted by the {@code execute} method.

我们平时在使用线程池的时候,都是直接 实例.execute(Runnable),一起跟进去,看看这个方法具体做了什么

public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */

        //结合上文的注释,我们得知,第一次,先判断当前的核心线程数,
        //如果小于初始化的值,马上创建;然后第二个if,将这个任务插入到工作线程,双重判断任务,
        //假定如果前面不能直接加入到线程池Worker集合里,则加入到workQueue队列等待执行。
        //里面的if else判断语句则是检查当前线程池的状态。如果线程池本身的状态是要关闭并清理了,
        //我们则不能提交线程进去了。这里我们就要reject他们。
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

所以其实主要起作用的还是addWorker()方法,我们继续跟踪进去:

private boolean addWorker(Runnable firstTask, boolean core) {
        ···多余代码


        try {
            w = new Worker(firstTask);  1.重点
            final Thread t = w.thread;
            if (t != null) {
                final ReentrantLock mainLock = this.mainLock;
                mainLock.lock();
                try {
                    // Recheck while holding lock.
                    // Back out on ThreadFactory failure or if
                    // shut down before lock acquired.
                    int rs = runStateOf(ctl.get());

                    if (rs < SHUTDOWN ||
                        (rs == SHUTDOWN && firstTask == null)) {
                        if (t.isAlive()) // precheck that t is startable
                            throw new IllegalThreadStateException();
                        workers.add(w);
                        int s = workers.size();
                        if (s > largestPoolSize)
                            largestPoolSize = s;
                        workerAdded = true;
                    }
                } finally {
                    mainLock.unlock();
                }
                if (workerAdded) {
                    t.start();   2. 重点
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w);
        }
        return workerStarted;
    }

我们看重点部分,其实最重要的是firstTask这个Runnable,我们一直跟踪这个对象就可以了,这个对象会new Worker(),那么这个wroker()就是一个包装类,里面带着我们实际需要执行的任务,后面进行一系列的判断就会执行t.start(); 这个t 就是包装类worker类里面的Thread,所以整个逻辑又转化进去Worker内部。

    private final class Worker
        extends AbstractQueuedSynchronizer
        implements Runnable
    {
        /**
         * This class will never be serialized, but we provide a
         * serialVersionUID to suppress a javac warning.
         */
        private static final long serialVersionUID = 6138294804551838833L;

        /** Thread this worker is running in.  Null if factory fails. */
        final Thread thread;
        /** Initial task to run.  Possibly null. */
        Runnable firstTask;

        /**
         * Creates with given first task and thread from ThreadFactory.
         * @param firstTask the first task (null if none)
         */
        Worker(Runnable firstTask) {
            setState(-1); // inhibit interrupts until runWorker
            this.firstTask = firstTask;
            this.thread = getThreadFactory().newThread(this);
        }

        /** Delegates main run loop to outer runWorker. */
        public void run() {
            runWorker(this);
        }

        ...省略代码
    }
  • 这个Worker包装类,重要的属性两个,thread 就是刚才上面那个方法执行的start()对象,这个thread又是把这个worker对象本身作为一个Runnable对象构建出来的,那么当我们调用thread.start()方法时候,实际调用的就是Worker类的run()方法。现在又要追踪进去,看这个runWorker(this),做的是什么鬼东西
    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 pool is stopping, ensure thread is interrupted;
                // if not, ensure thread is not interrupted.  This
                // requires a recheck in second case to deal with
                // shutdownNow race while clearing interrupt
                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);
        }
    }

这个方法还是比较好懂的: 
1. 一个大循环,判断条件是task != null || (task = getTask()) != null,task自然就是我们要执行的任务了,当task空而且getTask()取不到任务的时候,这个while()就会结束,循环体里面进行的就是task.run(); 
2.这里我们其实可以打个心眼,那基本八九不离十了,肯定是这个循环一直没有退出,所以才能维持着这一个线程不断运行,当有外部任务进来的时候,循环体就能getTask()并且执行。 
3.下面最后放getTask()里面的代码,验证猜想

   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) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }
  • 真相大白了,里面进行的也是一个死循环,主要看 Runnable r = timed ? 
    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : 
    workQueue.take();

  • 工作队列workQueue会一直去拿任务,属于核心线程的会一直卡在 workQueue.take()方法,直到拿到Runnable 然后返回,非核心线程会 workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) ,如果超时还没有拿到,下一次循环判断compareAndDecrementWorkerCount就会返回null,Worker对象的run()方法循环体的判断为null,任务结束,然后线程被系统回收

总结

一句话可以概述了,线程池就是用一堆包装住Thread的Wroker类的集合,在里面有条件的进行着死循环,从而可以不断接受任务来进行。

 

 

https://blog.csdn.net/cjh94520/article/details/70545202

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

线程池为什么能维持线程不释放,随时运行各种任务? 的相关文章

  • 如何调试罕见的死锁?

    我正在尝试调试一个自定义线程池实现rarely僵局 所以我不能使用像 gdb 这样的调试器 因为在出 现死锁之前我已经点击了 100 次 启动 调试器 目前 我正在 shell 脚本中的无限循环中运行线程池测试 但这意味着我看不到变量等 我
  • 使用 ThreadPoolExecutor 缩放 maxPoolSize;为什么池不动态增加其大小?

    作为初学者 我正在学习 java 中的线程和并发包 并且我已经阅读了有关 ThreadPoolExecutor 的文档来了解两者之间的差异getPoolSize getCorePoolSize getMaxPoolSize 并尝试在代码中实
  • 有没有办法确保线程分配给指定的对象集?

    我们正在开发一个应用程序 其中一组对象可能会受到来自 3 个不同源的消息的影响 每条消息 来自任何源 都有一个对象作为其目标 每个消息接收器将在其自己的线程上运行 我们希望消息的处理 接收后 尽可能高速 因此针对目标对象的消息处理将由线程池
  • async/await 如何在 ASP.Net 应用程序中提供帮助?

    在MVC控制器的action方法中使用async await可以扩展Web应用程序 因为在await时 Asp Net线程池的请求线程被释放 以便它可以为该工作进程处理IIS队列中的其他请求 这意味着 如果我们将工作进程的队列长度限制为 1
  • 多线程Windows服务的线程库[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如果我的接口必须返回任务,那么实现无操作的最佳方法是什么?

    在下面的代码中 由于接口 类LazyBar必须从其方法返回一个任务 并且出于论证原因不能更改 如果LazyBar它的实现很不寻常 因为它恰好快速且同步地运行 从该方法返回无操作任务的最佳方法是什么 我已经和Task Delay 0 下面 但
  • 两个ExecutorService可以共享一个线程池吗?

    我有一个记录集合要处理 并且处理可以并行化 所以我创建了一个执行服务 http java sun com j2se 1 5 0 docs api java util concurrent ExecutorService html via 执
  • 如何使用 ExecutorService Java 减少到达 Runnable 类的 run 方法的时间延迟

    我试图实现一个实时执行应用程序 其中按钮单击事件将任务分配给 Thread 该任务将调用MIDI方法播放一些音乐 单击按钮时必须立即开始播放音乐 并有一小段延迟 midi代码在Runnable类的run方法中实现 但是要在按钮单击事件本身发
  • 等待 QueueUserWorkItem 完成

    如果我将作业添加到线程池中QueueUserWorkItem 在所有工作完成之前 如何阻止我的计划继续进行 我知道我可以添加一些逻辑来阻止应用程序运行 直到所有作业完成 但我想知道是否有类似的东西Thread Join 或者是否有任何方法可
  • java中线程池的类型[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 java中的线程池有哪几种类型 我需要实现一个强大的多线程应用程序 该应用程序使用大量计算 我应该使用哪个线程池 java中有多种线程
  • 将 ThreadLocal 传播到从 ExecutorService 获取的新线程

    我正在一个单独的线程中运行一个带有超时的进程 使用 ExecutorService 和 Future 示例代码here https stackoverflow com questions 1164301 how do i call some
  • 黑莓中的队列线程

    我查看了 BB API 5 0 但找不到任何串行执行一批线程的方法 我知道 BB 对启动的线程数量有限制 所以如果用户点击速度足够快但我找不到像线程池这样的东西 我不想启动 7 是否有一个简单的解决方案 或者我是否必须创建一个数据结构 如果
  • 是否可以使用多线程而不需要一遍又一遍地创建线程?

    首先 再次感谢所有已经回答我问题的人 我不是一个经验丰富的程序员 这是我第一次体验多线程 我有一个与我的问题非常相似的例子 我希望这可以缓解我们的情况 public class ThreadMeasuring private static
  • 重写线程池中线程的中断方法

    假设我有这个 class Queue private static ExecutorService executor Executors newFixedThreadPool 1 public void use Runnable r Que
  • Play 框架:当请求超出可用线程时会发生什么

    我的线程池中有一个线程服务阻塞请求 def sync Action import Contexts blockingPool Future Thread sleep 100 Ok Done 在 Contexts blockingPool 中
  • 线程终止时的 Java ExecutorService 回调

    我正在使用缓存线程池 ExecutorService 来运行一些异步后台任务 我提供了 ThreadFactory 它将线程分发给 ExecutorService 只要需要它们 我对缓存线程池的理解是 线程空闲 60 秒后 它会被 Exec
  • 如何解决 MongoWaitQueueFullException?

    我运行一个java程序 它是一个线程执行程序 它将数千个文档插入到mongodb中的表中 我收到以下错误 Exception in thread pool 1 thread 301 com mongodb MongoWaitQueueFul
  • 为什么C++标准库中没有线程池? [复制]

    这个问题在这里已经有答案了 自 C 11 以来 C 中并行 并发编程工具的数量激增 线程 异步函数 并行算法 协程 但是流行的并行编程模式又如何呢 线程池 https en wikipedia org wiki Thread pool 据我
  • 不支持 STA 线程上多个句柄的 WaitAll

    为什么我会收到此错误消息 不支持 STA 线程上多个句柄的 WaitAll 我应该使用 STAThreadAttribute 属性吗 Update 不适用于 WPF 应用程序 笔记 错误位于 WaitHandle WaitAll doneE
  • 如何使用线程使 Windows 服务长期运行

    我有一个 win 服务托管一些我需要保持长时间运行的工作流程 一个 WorkflowApplication 和一个 WorkflowServiceHost 因为 OnStart 要求它完成并返回操作系统 所以我有一个在线程池中的另一个线程上

随机推荐

  • 第十四届蓝桥杯大赛软件赛省赛(C/C++ 大学B组)

    目录 试题 A 日期统计 1 题目描述 2 解题思路 3 模板代码 试题 B 01 串的熵 1 题目描述 2 解题思路 3 模板代码 试题 C 冶炼金属 1 题目描述 2 解题思路 3 模板代码 试题 D 飞机降落 1 题目描述 2 解题思
  • No implementation found for void com.wust.testjni10.CallJava.callPrintString()

    问题描述 当你看到这篇文章的时候 说明你会jni了 并且还在调用 so 库 可问题就出在调用的时候报了这么个错 No implementation found for void com wust testjni10 CallJava cal
  • uniapp-提现功能(demo)

    页面布局 提现页面 有一个输入框 一个提现按钮 一段提现全部的文字 首先用v model 和data内的数据双向绑定 输入框逻辑分析 输入框的逻辑 为了符合日常输出 所以要对输入框加一些条件限制 因为是提现 所以对输入的字符做筛选 只允许出
  • 由于某种原因,PowerPoint 无法加载MathType..... (亲测有效)

    网上找了较多的参考解决办法 最后发现如下博主提供的方法快捷有效 https blog csdn net dss875914213 article details 85873938 问题 PPT打开时弹出由于某种原因powerpoint无法加
  • 关于python

    1 关于python Python由荷兰数学和计算机科学研究学会的Guido van Rossum 于1990 年代初设计 作为一门叫做ABC语言的替代品 Python提供了高效的高级数据结构 还能简单有效地面向对象编程 Python语法和
  • Viva Workplace Analytics & Employee Feedback SU Viva Glint部署方案

    目录 一 Viva Workplace Analytics Employee Feedback SU Viva Glint介绍 二 Viva Glint和Viva Pulse特点和优势 1 简单易用
  • SLAM精度评定工具——EVO使用方法详解

    系统版本 Ubuntu20 04 ROS版本 Noetic EVO是用于处理 评估和比较里程计和SLAM算法的轨迹输出的工具 注意 本文的评测是在kitti数据集下进行评测 其他的数据集也支持评测 安装EVO 可以执行下面这条命令 pip
  • Pytorch 中如何对训练数据进行增强处理?

    假设我们的数据集是一个手写数字的图像数据集 其中每一张图像包含一个手写数字和对应的标签 我们可以通过随机旋转 平移 缩放和翻转等操作 对原始的图像进行变换增广 Data Augmentation 以增强模型的训练效果 举个例子 我们可以通过
  • JavaScript 实现数组中的字符串按长度排序,长度一样按字母顺序排序

    以下的newar数组里的val键值排序要求 字串按长度排序 长度一样按字母顺序排序 js实现数组中的字符串按长度排序 长度一样按字母顺序排序 function sortByLenByazAZVal array array sort a b
  • MYSQL之ON DUPLICATE KEY UPDATE使用

    创建表 DROP TABLE IF EXISTS user CREATE TABLE user id int 32 NOT NULL AUTO INCREMENT COMMENT 主键id userName varchar 32 NOT N
  • 【单片机毕业设计】【mcuclub-dz-055】基于单片机的智能手环控制系统设计

    最近设计了一个项目基于单片机的智能智能手环控制系统设计 与大家分享一下 一 基本介绍 项目名 智能手环 项目编号 mcuclub dz 055 单片机类型 STM32F103C8T6 具体功能 1 通过MAX30102测量心率 血氧 2 通
  • PFQ,适用于多核处理器系统中的网络监控框架

    PFQ 是一个支持多语言的网络框架 主要用于 Linux 操作系统下进行高效的包捕获和传输 适用于多核处理器系统中的网络监控框架 PFQ 专门为多核处理器而优化 包括对多个硬件队列的网络设备优化 支持任意网络设备驱动 并提供一个脚本用来加速
  • 动态内存与静态内存的区别

    1 静态内存 静态内存是指在程序开始运行时由编译器分配的内存 它的分配是在程序开始编译时完成的 不占用CPU资源 程序中的各种变量 在编译时系统已经为其分配了所需的内存空间 当该变量在作用域内使用完毕时 系统会 自动释放所占用的内存空间 变
  • Dropout层的个人理解和具体使用

    一 Dropout层的作用 dropout 能够避免过拟合 我们往往会在全连接层这类参数比较多的层中使用dropout 在训练包含dropout层的神经网络中 每个批次的训练数据都是随机选择 实质是训练了多个子神经网络 因为在不同的子网络中
  • 解决Google CoLab使用外部文件的问题

    有时 用Google CoLab时需要用到外部的数据集 这时 可以通过代码把文件上传到CoLab上 代码如下 from google colab import files uploaded files upload for fn in up
  • 2年python从业者整理的学习经验,手把手教你如何系统有效学习python

    我是从19年开始学python的 学了近半年 从业2年时间 看网课自己练 摸爬滚打后终于找到了一套不错的学习方法 以我个人经验给零基础入门的朋友讲下应该怎么系统有效学习python 首先 python不是洪湖猛兽 普通人只要认真学肯定是能学
  • python 尝试有道翻译

    一只小白的爬虫 写了一个简单 有道翻译 记录一下 如果大家有更好的方式 方法记得分享一下哦 coding utf 8 import urllib urllib2 json url http fanyi youdao com translat
  • iview引用自定义的图标

    iview引用自定义的图标 护花使者 博客园
  • C++图书管理系统(基于结构体数组)

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 前言 一 需求分析 二 代码实现 总结 前言 问题描述 要求以图书馆管理业务为背景 设计并实现一个 图书馆管理信息系统 软件 使用该系统可以方便查询图书的信息 借阅
  • 线程池为什么能维持线程不释放,随时运行各种任务?

    版权声明 本文为博主原创文章 未经博主允许不得转载 技术交流可邮 cjh94520 outlook com https blog csdn net cjh94520 article details 70545202 线程池 之前一直有这个疑