当前位置:网站首页>[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
边栏推荐
猜你喜欢

php-fpm软件的安装+openresty高速缓存搭建

了解小程序的笔记 2022/7/3

animation
![P1596 [USACO10OCT]Lake Counting S](/img/a7/07a84c93ee476788d9443c0add808b.png)
P1596 [USACO10OCT]Lake Counting S

About Wireshark's unsuccessful installation of npcap

Explain sizeof, strlen, pointer, array and other combination questions in detail

MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板

Ue5 opencv plug-in use

简易入手《SOM神经网络》的本质与原理

Abstract classes and interfaces
随机推荐
Mysql容器化(1)Docker安装MySQL
Why can void * be a general pointer
详解sizeof、strlen、指针和数组等组合题
Data analysis exercises
Image processing 8-cnn image classification
jupyter远程服务器配置以及服务器开机自启
Display terrain database on osgearth ball
Student educational administration management system of C # curriculum design
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
About Wireshark's unsuccessful installation of npcap
Unity editor expansion - the design idea of imgui
Osgearth topographic shading map drawing
Kunlunbase meetup is waiting for you!
【Rust 笔记】11-实用特型
Swagger document configuration
Golang time format sorting
Unity Editor Extension - event handling
Advanced OSG collision detection
matlab神经网络所有传递函数(激活函数)公式详解
[cloud native] introduction and use of feign of microservices