Parameter setting of java thread pool

There is no shortcut to success 2020-11-09 22:18:16
parameter setting java thread pool

Some candidates have been interviewed recently , During the interview , I prefer to ask some basic and open questions , It's used to see how the candidates' basic abilities are , For example, thread pool , The questioning process is generally similar to the following :

I'd like to ask the candidate how to use the thread pool , There are two results, no surprise :1. I don't use thread pools , I haven't been exposed to concurrency in my work ;2. use Executors.newCachedThreadPool;

Either way , I still hope to get their understanding of thread pool from candidates , So I still ask ThreadPoolExecutorService Several parameters related to , Except for candidates with a very poor foundation , The average candidate can answer corePoolSize, maxPoolSize, BlockingQueue The relationship between :

  • corePoolSize It's the number of core threads ,maxPoolSize Is the maximum number of threads ,BlockingQueue Is task queue
  • When a task is submitted , First create corePoolSize Number of threads , There are more tasks to go into BlockingQueue,BlockingQueue If it is not enough, create the number of threads until macPoolSize
  • After a period of idle time, the thread will be destroyed until only... Is left in the thread pool core Number of threads

After answering the function of these parameters and their relationship , I usually give one or two thread pool related questions , See if the candidate can think of , such as :

  • If the task submitted to the thread pool ,IO The percentage of time spent is 90%, The percentage of computing time 10%, Ignore the number of tasks committed to the thread pool , stay 4C8G On the machine , Ideally, how many threads are created in the thread pool is optimal

Usually, I only write CURD Candidate , It's hard to calculate , Of course, I have met many people who can work out the results , Number of threads =(1/0.1) * 4 = 40, The candidate who can figure it out , I'll ask other questions , for example :

  • If e There is a kind of cpu Intensive tasks , No, IO operation , In daily life, only 1 A mission , There will be peak traffic 50 A mission ,4C8G On the machine , Used thread pool , How to set up corePoolSize, maxPoolSize as well as BlockingQueue Size

Questions like this , I haven't met a candidate who can answer , Generally, the answer is similar to the following :

  • corePoolSize=4 Threads ,maxPoolSize Bigger 100 individual , The queue is bigger ,1 ten thousand
  • 2 Threads , The queue is bigger 1 thousand

So I'll give some guidance to the candidates , For example, answer core=4, max=50, queue=1w Of , I'll ask him what he set up maxPoolSize Does it work , The obvious queue is set to 1w, This line is too big , It's not full at all ,maxPoolSize The number of threads will never be created , Obviously, the candidates are randomly set up , Without thinking , At this time, I will ask him to think again with the previous question

After this step , Some of the waiting people began to answer 4 Threads , queue 50, But it's not optimal , Because every second of the day 1 A task , Just one thread is enough , Create 4 Threads , There is 3 A waste

Obviously , Only one thread is needed everyday , that corePoolSize=1, And the rush hour , Although the mission has 50 individual , But it's just 4C Machine , about cpu Intensive task ,4 Threads are optimal , So ideally maxCorePoolSize=4, Finally, let's look at the queue , Because the queue is full ,max Will be created , And we need to make max Quickly created , There's no mission rejection , therefore , The queue size can be set to 46, The thread pool behaves as follows :

  • Submit the first task , Create core,1 Threads
  • Submit the second to 47 A task , These tasks go into the queue , At this point the queue is full
  • Submit No 48 Mission to 50 A task , Create max, There is a total of 4 Threads
  • 4 At the same time, the threads in the queue will be 46 I've finished the task
  • After a while ,max - core Number of threads destroyed , That is to destroy 3 Threads , There is one thread left
本文为[There is no shortcut to success]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云