码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • java 线程池执行流程源码讲解


    1. threadPoolExecutor.execute(); //执行过程。
    2. public void execute(Runnable command) {
    3. if (command == null)
    4. throw new NullPointerException();
    5. *
    6. * Proceed in 3 steps:
    7. *
    8. * 1. If fewer than corePoolSize threads are running, try to
    9. * start a new thread with the given command as its first
    10. * task. The call to addWorker atomically checks runState and
    11. * workerCount, and so prevents false alarms that would add
    12. * threads when it shouldn't, by returning false.
    13. *
    14. * 2. If a task can be successfully queued, then we still need
    15. * to double-check whether we should have added a thread
    16. * (because existing ones died since last checking) or that
    17. * the pool shut down since entry into this method. So we
    18. * recheck state and if necessary roll back the enqueuing if
    19. * stopped, or start a new thread if there are none.
    20. *
    21. * 3. If we cannot queue task, then we try to add a new
    22. * thread. If it fails, we know we are shut down or saturated
    23. * and so reject the task.
    24. *
    25. //获取运行线程数
    26. int c = ctl.get();
    27. //workerCountOf(c)获取工作线程个数是否小于核心线程数
    28. if (workerCountOf(c) < corePoolSize) {
    29. //增加addWorker(command, true) 增加addWorker 第二个线程数是否核心
    30. if (addWorker(command, true))
    31. return;
    32. c = ctl.get();
    33. }
    34. //如果大于核心线程数,判断是否运行,队列放入command
    35. if (isRunning(c) && workQueue.offer(command)) {
    36. int recheck = ctl.get();
    37. 没有运行线程,且可以进行队列出队列,执行拒绝策略(因为上次检查后已有的死亡)
    38. if (!isRunning(recheck) && remove(command))
    39. reject(command);
    40. 如果工作
    41. else if (workerCountOf(recheck) == 0)
    42. addWorker(null, false);
    43. }
    44. //如果添加非核心线程失败,走拒绝策略
    45. else if (!addWorker(command, false))
    46. reject(command);
    47. }

  • 相关阅读:
    温度、机械振动等对电子产品的影响
    光谱图像超分辨率综述
    人工智能在网络安全中的重要性
    ArduinoUNO实战-第十一章-4位共阴数码管实验
    计算机毕业设计(附源码)python智能旅游电子票务系统
    MIPS指令和汇编语言有什么关系
    Python制作GUI学生管理系统毕设,大学生总会用得到
    中石化、中石油接口文档源码分享
    【SNMP】snmp trap 与介绍、安装、命令以及Trap的发送与接收java实现
    AC自动机(查询)
  • 原文地址:https://blog.csdn.net/sai739295732/article/details/127654261
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号