当前位置:网站首页>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
边栏推荐
- Markdown基础语法
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- 拦截器与过滤器的区别
- [100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
- How can retail enterprises open the second growth curve under the full link digital transformation
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- Use cheat engine to modify money, life and stars in Kingdom rush
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- 在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
- R language ggplot2 visualization: gganimate package creates dynamic histogram animation (GIF) and uses transition_ The States function displays a histogram step by step along a given dimension in the
猜你喜欢

Stm32g0 USB DFU upgrade verification error -2

新手必看,点击两个按钮切换至不同的内容

Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent

机器学习笔记 - 时间序列预测研究:法国香槟的月销量

为什么要做企业固定资产管理系统,企业如何加强固定资产管理

In pytorch function__ call__ And forward functions

Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base

MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree

Have you stepped on the nine common pits in the e-commerce system?

Use cheat engine to modify money, life and stars in Kingdom rush
随机推荐
C的内存管理
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
[test development] takes you to know what software testing is
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
The R language dplyr package rowwise function and mutate function calculate the maximum value of multiple data columns in each row in the dataframe data, and generate the data column (row maximum) cor
metric_logger小解
UML class diagram
使用CLion编译OGLPG-9th-Edition源码
预处理和预处理宏
新手必看,点击两个按钮切换至不同的内容
9D电影是怎样的?(+维度空间常识)
虚拟机初始化脚本, 虚拟机相互免秘钥
在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
全志A33使用主线U-Boot
【测试开发】一文带你了解什么是软件测试