线程阻塞jdk线程池的核心代码

改章节是一篇关于线程阻塞的帖子

    ThreadPoolExecutor的核心部份主要有以下几点:

    1 线程池中工作线程的数量,源码的execute方法中有如下记载:

         * 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.
         */

    每日一道理
站在历史的海岸漫溯那一道道历史沟渠:楚大夫沉吟泽畔,九死不悔;魏武帝扬鞭东指,壮心不已;陶渊明悠然南山,饮酒采菊……他们选择了永恒,纵然谄媚诬蔑视听,也不随其流扬其波,这是执著的选择;纵然马革裹尸,魂归狼烟,也要仰天长笑,这是豪壮的选择;纵然一身清苦,终日难饱,也愿怡然自乐,躬耕陇亩,这是高雅的选择。在一番选择中,帝王将相成其盖世伟业,贤士迁客成其千古文章。

    

    2 任务队列,线程池维护的是一个阻塞队列,BlockingQueue<Runnable> workQueue,用来保存task,这个阻塞很主要。

文章结束给大家分享下程序员的一些笑话语录: 问路
有一个驾驶热气球的人发现他迷路了。他降低了飞行的高度,并认出了地面 上的一个人。他继续下降高度并对着那个人大叫,“打扰一下,你能告诉我我 在哪吗?”
下面那个人说:“是的。你在热气球里啊,盘旋在 30 英尺的空中”。
热气球上的人说:“你一定是在 IT 部门做技术工作”。
“没错”,地面上的人说到,“你是怎么知道的?”
“呵呵”,热气球上的人说,“你告诉我的每件事在技术上都是对的,但对都没 有用”。
地面上的人说,“你一定是管理层的人”。
“没错”,热气球上的人说,“可是你是怎么知道的?”
“呵呵”,地面上的那人说到,“你不知道你在哪里,你也不知道你要去哪,你 总希望我能帮你。你现在和我们刚见面时还在原来那个地方,但现在却是我 错了”。

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3080473.html