当前位置:网站首页>Thread application instance
Thread application instance
2022-07-02 19:16:00 【Sang Yinghao】
One . Wait timeout mode
scene : Wait for a period of time when calling a method , If this method can get results within a given time , Will return immediately , conversely , If timeout, return the default result .
Two . Database connection pool
Definition of connection pool : Constructor initializes the maximum limit of the connection , Maintain connections through a two-way queue , The caller needs to call fetchConnection(long) Method to specify how many milliseconds to timeout to get a connection , When the connection is used , Need to call releaseConnection(Connection) Method to put the connection back into the connection pool .
3、 ... and . Thread pool technology
3.1 Role of thread pool
face Thousands of people Service delivery hand over Into the clothing service device when , If you still use one service One Line The way of Cheng , Then it will gen Build tens of thousands remember Of Line cheng , this It's not a good choice , Because this will make the operating system switch thread context frequently , Increase the load of the system for no reason , The creation and extinction of threads require system resources , It also undoubtedly wastes system resources .Thread pool solves this problem well , It creates several threads in advance , And it can't be directly controlled by users to create threads
3.2 Advantages of thread pool
- Reduce resource consumption
- Improve response time
- Improve the manageability of threads
3.3 Several parameters in the thread pool
1: Number of core threads (corePoolSize)
The design of the number of core threads needs to be determined according to the processing time of the task and the number of tasks generated per second , for example : Performing a task requires 0.1 second , The system is... Percent 80 Every second of the time will produce 100 A mission , So if you want to be in 1 Finish this in seconds 100 A mission , Need 10 Threads , At this point, we can design the number of core threads as 10; Of course, the actual situation cannot be so average , So we usually follow 8020 Principle design is enough , Both according to percent 80 Design the number of core threads in the case of , The remaining percent 20 The maximum number of threads can be used to process ;
2: Task queue length (workQueue)
The length of task queue is generally designed as : Number of core threads / Execution time of single task *2 that will do ; For example, in the above scene , The number of core threads is designed as 10, The execution time of a single task is 0.1 second , The queue length can be designed as 200;
3: Maximum number of threads (maximumPoolSize)
The design of the maximum number of threads needs to refer to the conditions of the number of core threads , You also need to refer to the maximum number of tasks generated by the system per second : for example : In the above environment , If the maximum task generated by the system per second is 1000 individual , that , Maximum number of threads =( Maximum number of tasks - Task queue length )* Execution time of single task ; Both : Maximum number of threads =(1000-200)*0.1=80 individual ;
4: Maximum free time (keepAliveTime)
The design of this parameter completely refers to the system operating environment and hardware pressure setting , There is no fixed reference value , The user can set a reasonable value according to experience and the time interval of the task generated by the system ;
3.4 Thread pool workflow
3.5Executor and Executors The difference between
Executor Interface objects can perform our threading tasks ;
Executors Different methods of tool classes create different Thread pool , To meet the needs of the business .
ExecutorService Interface inherited Executor Interface and extended , More ways , We can get the execution status of the task and the return value of the task .
3.6 Do you know how to create a thread pool ?
ThreadPoolExecutor
边栏推荐
- R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
- 新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
- Yunna | why use the fixed asset management system and how to enable it
- How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
- R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
- Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
- juypter notebook 修改默认打开文件夹以及默认浏览器
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
- The difference between interceptor and filter
猜你喜欢

UML class diagram
![[daily question] the next day](/img/8a/18329bd9b4a3a4445c8fbbc1ce562b.png)
[daily question] the next day

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5

论文导读 | 机器学习在数据库基数估计中的应用

【JVM调优实战100例】01——JVM的介绍与程序计数器

数据降维——因子分析

全链路数字化转型下,零售企业如何打开第二增长曲线

Installation of thingsboard, an open source IOT platform
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

【JVM调优实战100例】03——JVM堆调优四例
随机推荐
High frequency interview questions
What is 9D movie like? (+ common sense of dimension space)
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
Talk about the design of red envelope activities in e-commerce system
ICDE 2023|TKDE Poster Session(CFP)
Processing strategy of message queue message loss and repeated message sending
高频面试题
Progress progress bar
【JVM调优实战100例】01——JVM的介绍与程序计数器
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
Imitation Jingdong magnifying glass effect (pink teacher version)
LightGroupButton* sender = static_ cast<LightGroupButton*>(QObject::sender());
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
医院在线问诊源码 医院视频问诊源码 医院小程序源码
聊聊电商系统中红包活动设计
How to enable the run dashboard function of idea
UML class diagram
新手必看,點擊兩個按鈕切換至不同的內容