当前位置:网站首页>After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
2022-07-03 11:23:00 【androidstarjack】
Click on the top “ Terminal R & D department ”
Set to “ Star standard ”, Master more database knowledge with you One 、 How to configure a reasonable number of threads in a thread pool
(1)CPU intensive :
Definition :CPU Intensive means that the task requires a lot of computation , And there's no blocking ,CPU Running at full speed .
CPU Intensive tasks are only in real multicore CPU It's possible to speed up ( Through multi-threading ).
CPU Configure as few threads as possible for intensive tasks .
“CPU Configuration formula for the number of intensive threads :(CPU Check the number +1) A pool of threads
(2)IO intensive :
Definition :IO intensive , That is, the task requires a lot of IO, A lot of congestion .
Running on a single thread IO Intensive tasks can lead to a waste of CPU Computing power is wasted waiting .
therefore IO Multithreading in intensive tasks can greatly speed up the running of programs , Even in a single core CPU On , This acceleration mainly takes advantage of the wasted blocking time .
The first configuration :
because IO Intensive task threads do not always perform tasks , Configure as many threads as possible .
“ Configuration formula :CPU Check the number * 2.
Second configuration mode :
IO Intensive time , Most threads are blocked , Therefore, it is necessary to configure multiple threads .
“ Configuration formula :CPU Check the number / (1 – Block coefficient )(0.8~0.9 Between )
such as :8 nucleus / (1 – 0.9) = 80 Number of threads
Two 、 Custom thread pool
package com.lxk.com;
import java.util.concurrent.*;
/**
* Custom thread pool
*/
public class ThreadPoolDemoFour {
public static void main(String[] args) {
// Custom thread pool
ExecutorService threadPool = new ThreadPoolExecutor(3,
5,
60,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
try {
for (int i = 0; i < 10; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t Handle the business ");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
}3、 ... and 、 The actual use JDK Which thread pool is built in ?
【 mandatory 】 Thread resources must be provided through the thread pool , It is not allowed to explicitly create threads in the application . explain : The advantage of using thread pool is to reduce the time consumed in creating and destroying threads and the overhead of system resources , Solve the problem of insufficient resources . If you do not use thread pools , It may cause the system to create a large number of similar threads, leading to memory consumption or “ Over switching ” The problem of .
【 mandatory 】 Thread pools are not allowed Executors To create , But through ThreadPoolExecutor The way , This way of processing makes the students who write more clear about the running rules of the thread pool , Avoid the risk of resource depletion . explain :Executors The disadvantages of the returned thread pool object are as follows :
FixedThreadPool and SingleThreadPool: The allowed request queue length is Integer.MAX_VALUE, A large number of requests may pile up , Which leads to OOM.
CachedThreadPool and ScheduledThreadPool: The number of threads allowed to be created is Integer.MAX_VALUE, A large number of threads may be created , Which leads to OOM.

source :lixinkuan.blog.csdn.net/article/details/94501073
And finally ( Please pay attention to , Don't go whoring me for nothing )
If this article is helpful to you , Or if there's some inspiration , Help scan and issue QR code, pay attention to , Your support is my biggest motivation for writing .
Ask for one key and three links : give the thumbs-up 、 forward 、 Looking at .
Write it at the end
You can leave a message below to discuss what you don't understand , You can also ask me by private message. I will reply after seeing it . Finally, if the article is helpful to you, please remember to give me a like , Pay attention and don't get lost
@ Terminal R & D department
Fresh dry goods are shared every day !
reply 【idea Activate 】 You can get idea How to activate
reply 【Java】 obtain java Relevant video tutorials and materials
reply 【SpringCloud】 obtain SpringCloud Many relevant learning materials
reply 【python】 Get the full set 0 Basics Python Knowledge Manual
reply 【2020】 obtain 2020java Related interview questions tutorial
reply 【 Add group 】 You can join the technical exchange group related to the terminal R & D department
Read more
use Spring Of BeanUtils front , I suggest you understand these pits first !
lazy-mock , A lazy tool for generating backend simulation data
In Huawei Hongmeng OS Try some fresh food , My first one “hello world”, take off !
The byte is bouncing :i++ Is it thread safe ?
One SQL Accidents caused by , Colleagues are fired directly !!
Too much ! Check Alibaba cloud ECS Of CPU Incredibly reach 100%
a vue Write powerful swagger-ui, A little show ( Open source address attached )
Believe in yourself , Nothing is impossible , Only unexpected, not only technology is obtained here !
If you like, just give me “ Looking at ”边栏推荐
- ASP.NET-酒店管理系统
- Google Earth Engine(GEE)——GHSL 全球人口网格数据集250米分辨率
- Tencent micro app to get wechat user information
- FL Studio 20无限试用版水果编曲下载
- Linear table sequence table comprehensive application problem p18
- The element form shows the relationship between elementary transformation and elementary matrix
- Using activity to realize a simple inputable dialog box
- 程序员的创业陷阱:接私活
- .\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
- ConstraintLayout跟RelativeLayout嵌套出现的莫名奇妙的问题
猜你喜欢

MATLAB提取不规则txt文件中的数值数据(简单且实用)

ASP.NET-酒店管理系统

How did I grow up in the past eight years as a test engineer of meituan? I hope technicians can gain something after reading it

【Proteus仿真】74HC154 四线转12线译码器组成的16路流水灯

面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩

【obs】obs的ini格式的ConfigFile

10. Nacos source code construction

Incremental database backup - DB incr DB full

Application of high-precision indoor positioning technology in safety management of smart factory

Arctangent entropy: the latest SCI paper in July 2022
随机推荐
数据库增量备份 - DB INCR DB FULL
线性表的双链表
Linear table sequence table comprehensive application problem p18
程序进程管理工具-go supervisor
[proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder
LeetCode 46:全排列
Ext file system mechanism principle
Inexplicable problems in the nesting of constraintlayout and relativelayout
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years
Project management essence reading notes (VII)
Stack, monotone stack, queue, monotone queue
Matlab extracts numerical data from irregular txt files (simple and practical)
Definition and properties of summation symbols
The testing department of the company came to the king of the Post-00 roll, and the veteran exclaimed that it was really dry, but
活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点
反正切熵(Arctangent entropy):2022.7月最新SCI论文
The manuscript will be revised for release tonight. But, still stuck here, maybe what you need is a paragraph.
Solve undefined reference to`__ aeabi_ Uidivmod 'and undefined reference to`__ aeabi_ Uidiv 'error