Four kinds of strategies refused thread pool

1.ThreadPoolExecutor ExecutorService class implements the interface and the Executor interface, you can set the thread pool corePoolSize, maximum thread pool size, AliveTime, refused strategies. Common constructors :
the ThreadPoolExecutor (corePoolSize int, int maximumPoolSize,
Long keepAliveTime, TimeUnit Unit, BlockingQueue workQueue,
RejectedExecutionHandler handler )

corePoolSize: thread pool to maintain a minimum number of threads
maximumPoolSize: thread pool to maintain the maximum number of threads
keepAliveTime: thread pool maintenance thread allowed idle time
(explanation: When the number of thread pool of more than corePoolSize, excess idle threads of survival time)
unit: the thread pool maintenance thread idle time allowed for the unit
workQueue: buffer queue thread pool used
handler: thread pool to handle the task of policy that denies

Here Insert Picture Description
there are four choices handler:

Strategy 1: ThreadPoolExecutor.AbortPolicy ()
throws java.util. concurrent .RejectedExecutionException abnormal, for example:
Here Insert Picture Description
small question : The author of the operating results still have some questions about why an exception under the third line appeared
Here Insert Picture Description

Strategy 2: ThreadPoolExecutor.CallerRunsPolicy
A handler for rejected tasks that runs the rejected task directly in the calling thread of the execute method; if the executor has been shut down, the task is discarded. As follows :( little doubt : The author of the operating results there are still many questions, such as pool-1-thread-7 is running continuously appeared many times, this is why)?
Here Insert Picture Description
Explanation: The above zxai
Strategy 3 : RejectedExecutionHandler handler =
New new ThreadPoolExecutor.DiscardOldestPolicy ();
this operation result would not have all the threads 100 is performed. Processing source code as follows:
some of the commonly used methods in Java Queue:
poll () Removes and returns to ask the head of the queue element
Here Insert Picture Description
to explain : This policy will discard the oldest one request, that is a task to be executed, and try to submit again the current task. (Q: Why is the first element of the queue pops up? Answer: because the FIFO queue, so the head of the queue element is certainly the oldest one request, and DiscardOldestPolicy strategy is to repeal the oldest request, this opportunity (equivalent to grab the oldest request of the mouth to eat meat) left to submit a new current inside the thread pool thread (this time the thread pool is full, the queue is full also, if dissatisfied, will not have rejected the policy!))

Strategy 4 : ThreadPoolExecutor.DiscardPolicy
A handler for rejected tasks, it discards the rejected task by default.
Operating results will not perform all 100 threads.
Source follows, is in fact not operate on the thread:
Here Insert Picture Description
Here Insert Picture Description

Original Address: https: //blog.csdn.net/qq_40241957/article/details/85468994

Guess you like

Origin www.cnblogs.com/jpfss/p/11671127.html