当前位置:网站首页>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 ”
边栏推荐
- ORACLE 11G 单机冷备数据库
- Analysis of JMM memory model
- 10. Nacos source code construction
- C语言日志库zlog基本使用
- Google Earth Engine(GEE)——GHSL 全球人口网格数据集250米分辨率
- C language two-dimensional array
- 使用onvif协议操作设备
- MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)
- Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
- Static library vs shared library
猜你喜欢
12. Nacos server service registration of source code analysis of Nacos service registration
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
EPS电动转向系统分析
封装一个koa分布式锁中间件来解决幂等或重复请求的问题
Arctangent entropy: the latest SCI paper in July 2022
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
高精度室内定位技术,在智慧工厂安全管理的应用
Software testing e-commerce projects that can be written into your resume, don't you come in and get it?
随机推荐
软件测试工程师的5年之痒,讲述两年突破瓶颈经验
Processes and threads
Reading notes: heart like Bodhi, Cao Dewang
Leetcode 46: full arrangement
Kotlin's use of the no Arg compiler plug-in in gradle
栈,单调栈,队列,单调队列
Project management essence reading notes (6)
Intel 13th generation core flagship exposure, single core 5.5ghz
AMS series - application startup process
今晚要修稿子準備發佈。但是,仍卡在這裡,也許你需要的是一個段子。
程序进程管理工具-go supervisor
Empire CMS no thumbnail smart tag (e:loop) two ways to judge whether there is a titlepic
AIDL
Imread change image display size
线性表的双链表
Illustrated network: what is virtual router redundancy protocol VRRP?
Struct function & function pointer
Double linked list of linear list
项目管理精华读书笔记(七)
ASP.NET-酒店管理系统