当前位置:网站首页>[concurrent programming] working mechanism and type of thread pool
[concurrent programming] working mechanism and type of thread pool
2022-07-03 08:32:00 【keeper42】
JDK Thread pool and working mechanism in
Creation of thread pool
ThreadPoolExecutor,jdk Parent class of all thread pool implementations
Meaning of each parameter
int corePoolSize : Number of core threads in the thread pool ,< corePoolSize , New threads will be created ,= corePoolSize , This task will be saved to BlockingQueue, If the prestartAllCoreThreads() The method will start at once corePoolSize Number of threads .
int maximumPoolSize, Maximum number of threads allowed ,BlockingQueue It's full, too ,< maximumPoolSize It will create a new thread again
long keepAliveTime, When the thread is idle , Time to live , This parameter is only in > corePoolSize Only useful
TimeUnit unit, Unit value of survival time
BlockingQueue<Runnable> workQueue, Save the blocking queue for the task
ThreadFactory threadFactory, A factory for creating threads , Give the new thread a name
RejectedExecutionHandler handler : Saturated strategy
AbortPolicy : Throw an exception directly , Default ;
CallerRunsPolicy: Use the thread of the caller to execute the task
DiscardOldestPolicy: Discard the oldest task in the blocking queue , The top task in the queue
DiscardPolicy : The current task is discarded directly
Implement your own saturation strategy , Realization RejectedExecutionHandler Interface can
Submit tasks
execute(Runnable command) No need to return
Future<T> submit(Callable<T> task) Need to return
Close thread pool
shutdown(),shutdownNow();
shutdownNow(): Set the state of thread pool , It also tries to stop the thread that is running or suspending the task
shutdown() Set the state of thread pool , It will only interrupt all threads that do not execute tasks
Working mechanism ( Interview questions )

Reasonable configuration of thread pool ( Interview questions )
According to the nature of the task : Computationally intensive (CPU),IO intensive , mixed type
Computationally intensive : encryption , Large number decomposition , Regular ……., The number of threads should be a little smaller , Best recommendation : Mechanical Cpu The core number +1, Why? +1, Prevent missing pages ,( Mechanical Cpu The core =Runtime.getRuntime().availableProcessors();)
IO intensive : Read the file , Database connection , Network communication , The number of threads should be a little larger , Mechanical Cpu The core number *2,
mixed type : Try to split ,IO intensive >> Computationally intensive , It doesn't mean much to break up ,IO intensive ~ Computationally intensive
The choice of the queue , Should use bounded , Unbounded queues can cause memory overflows ,OOM
Predefined thread pools
FixedThreadPool
Create a fixed number of threads , It is suitable for servers with heavy load , Unbounded queue is used
SingleThreadExecutor
Create a single thread , It is necessary to ensure the execution of tasks in sequence , There won't be multiple threads active , Unbounded queue is used
CachedThreadPool
New threads will be created as needed , Programs that perform many short-term asynchronous tasks , Used SynchronousQueue
WorkStealingPool(JDK7 in the future )
be based on ForkJoinPool Realization
ScheduledThreadPoolExecutor
Periodic tasks need to be performed on a regular basis ,Timer Not recommended .
newSingleThreadScheduledExecutor: Contains only one thread , Only a single thread is required to perform periodic tasks , Ensure that the tasks are performed in sequence
newScheduledThreadPool Can contain multiple threads , Threads perform periodic tasks , When controlling the number of threads in the background appropriately
边栏推荐
- 【Rust 笔记】09-特型与泛型
- Development experience and experience
- Advanced OSG collision detection
- Conversion between golang JSON format and structure
- About Wireshark's unsuccessful installation of npcap
- [audio and video] ijkplayer error code
- Osganimation library parsing
- Cesium service deployment, and import and display local 3dfiles data
- UE4 plug in development
- Initial unity
猜你喜欢

Data analysis exercises

100 GIS practical application cases (78) - Multi compliance database design and data warehousing

Unity editor expansion - controls, layouts

Graphics_ Learnopongl learning notes

GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库

Notes on understanding applets 2022/7/3

Unity editor expansion - scrolling list

基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程

Advanced OSG collision detection

十六进制编码简介
随机推荐
Constraintlayout's constraintset dynamically modifies constraints
Use of ue5 QRcode plug-in
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
Flex flexible box layout
Unity4.3.1 engine source code compilation process
Introduction to hexadecimal coding
单调栈-42. 接雨水
Delete the last character of the string in golang
详解sizeof、strlen、指针和数组等组合题
Minimap plug-in
十六进制编码简介
C#课程设计之员工信息管理系统
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
【Rust笔记】05-错误处理
Golang 字符串分割,替换和截取
[audio and video] ijkplayer error code
Simply start with the essence and principle of SOM neural network
Message pack in C deserializes array objects
Golang string segmentation, substitution and interception
Introduction to Base64 coding