In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to parse ThreadPoolExecutor. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The thread pool class is java.util.concurrent.ThreadPoolExecutor, and the common construction methods are:
ThreadPoolExecutor (intcorePoolSize, int maximumPoolSize
LongkeepAliveTime, TimeUnit unit, BlockingQueueworkQueue
RejectedExecutionHandlerhandler)
CorePoolSize: the minimum number of threads maintained by the thread pool
MaximumPoolSize: the maximum number of threads maintained by the thread pool
KeepAliveTime: thread pool maintains the idle time allowed by threads
Unit: the thread pool maintains the unit of idle time allowed by threads.
WorkQueue: the buffer queue used by the thread pool
Handler: thread pool strategy for rejecting tasks
A task is added to the thread pool through the execute (Runnable) method. The task is an object of type Runnable, and the execution method of the task is the run () method of the object of type Runnable.
When a task is to be added to the thread pool through the execute (Runnable) method:
1. If the number of threads in the thread pool is less than corePoolSize at this time, even if all the threads in the thread pool are idle, create new threads to handle the added tasks.
2. If the number of threads in the thread pool is equal to corePoolSize, but the buffer queue workQueue is not full, then the task is placed in the buffer queue.
3. If the number of threads in the pool is greater than corePoolSize, the buffer queue workQueue is full, and the number of threads in the pool is less than maximumPoolSize, create new threads to handle the added tasks.
4. If the number in the thread pool is greater than corePoolSize, the buffer queue workQueue is full, and the number in the thread pool is equal to maximumPoolSize, then the task is handled through the policy specified by handler.
The priority of processing tasks is:
Core thread corePoolSize, task queue workQueue, and maximum thread maximumPoolSize, if all three are full, use handler to process rejected tasks.
When the number of threads in the thread pool is greater than corePoolSize, if a thread is idle longer than keepAliveTime, the thread will be terminated. This allows the thread pool to dynamically adjust the number of threads in the pool.
The optional parameters for unit are several static properties in java.util.concurrent.TimeUnit:
NANOSECONDS
MICROSECONDS
MILLISECONDS
SECONDS
WorkQueue I often use is: java.util.concurrent.ArrayBlockingQueue
Handler has four options:
1. ThreadPoolExecutor.AbortPolicy ()
Throw a java.util.concurrent.RejectedExecutionException exception
2. ThreadPoolExecutor.CallerRunsPolicy ()
Try adding the current task again, and he will automatically repeat the call to the execute () method
3. ThreadPoolExecutor.DiscardOldestPolicy ()
Abandon the old task
4. ThreadPoolExecutor.DiscardPolicy ()
Abandon the current task
Example:
Import java.util.concurrent.ArrayBlockingQueue
Import java.util.concurrent.ThreadPoolExecutor
Import java.util.concurrent.TimeUnit
Public class TestThreadPool {
Private static int produceTaskSleepTime = 2
Private static int produceTaskMaxNumber = 10
Public static void main (String [] args) {
/ / construct a thread pool
ThreadPoolExecutor threadPool = new ThreadPoolExecutor (2,4,3, TimeUnit.SECONDS)
New ArrayBlockingQueue (3), new ThreadPoolExecutor.DiscardOldestPolicy ()
For (int I = 1; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.