ThreadPoolExecutor series & lt; 1. ThreadPoolExecutor Mechanism & gt ;,

Source: Internet
Author: User

ThreadPoolExecutor series <1. ThreadPoolExecutor mechanism>,

Solution:

1. when processing a large number of asynchronous tasks, the resource overhead of each task can be reduced;

2. threads are managed through the thread pool to reduce thread resource overhead;

3. count the number of completed tasks and the number of active threads;

I personally think the key is how the thread pool and task queue work perfectly together.

ThreadPoolExecutor Mechanism

 

1. Existing threads in the thread pool <corePoolSize (basic thread in the thread pool)

I> ensure that the number of threads in the thread pool reaches corePoolSize (except allowCoreThreadTimeOut = true)

Ii> if a new task submitted is submitted, the thread pool starts a new thread to process the task even if the existing thread is in the idle state;

Iii> if a new task is submitted (new task submitted), the thread pool (Executor) First enables the thread to process tasks, rather than entering the queue,

Until current threads> = corePoolSize, the new task first enters the queue and then waits for processing;

 

2. corePoolSize (basic thread in the thread pool) <existing thread in the thread pool <maximumPoolSize (maximum thread in the thread pool)

I> if a new task (new task submitted) is submitted, a new thread is started to process the thread pool only when the queue is full,

Otherwise, the new task will be processed only after the current thread finishes processing the task;

Ii> If the thread residence time (Keep-alive time) is exceeded, the exceeding thread (currrent size-corePoolSize) is terminated and resources are released;

SetKeepAliveTime dynamically adjusts the residence idle time;


3. Existing threads in the thread pool (current threads)> maximumPoolSize (maximum thread in the thread pool)

I> if a new task is submitted (new task submitted), the thread pool will still create a new thread, but the new task is rejected (rejected );

 

New task rejected processing policy:

A> ThreadPoolExecutor. AbortPolicy: a RejectedExecutionException is thrown during running)

B> ThreadPoolExecutor. CallerRunsPolicy: the thread re-processes the request of the task through a simple feedback control mechanism, slowing down the time when the task enters the queue,
This allows the thread to process tasks faster than the task to reach the queue;

Advantage: Do not throw an exception to interrupt the thread and do not lose the task. For example:

 

C> ThreadPoolExecutor. DiscardPolicy: directly throw the task.

D> ThreadPoolExecutor. DiscardOldestPolicy: In the order of entry, discard the first task to enter the other end of the queue, free up space, and then add new tasks to the queue;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.