Custom thread pool usage details

Source: Internet
Author: User
Tags throw exception

The key to what type of queue is this method of construction

  when a bounded queue is used , if a new task is required to execute, if the thread pool actual thread count is less than corepoolsize, priority is given to creating threads, if greater than corepoolsize, the task will be queued, if the team is full, If the number of threads is not greater than maximumpoolsize, a new thread is created and a deny policy is executed if the number of thread is greater than maximumpoolsize. or other custom methods.

  Unbounded task queue,linkedblockingqueue. The unbounded task queue does not have a task queued failure compared to a bounded queue unless the system resources are exhausted. When a new task arrives, the number of threads in the system is less than corepoolsize. The new thread executes the task. When the corepoolsize is reached, it will not continue to increase. If there is still a new task to join, and there is no idle thread resource, then the task goes directly to the queue waiting. If the speed of task creation and processing varies greatly, the unbounded queue will continue to grow rapidly until the system memory is exhausted.

JDK rejection policy:

AbortPolicy: Direct throw exception organization system normal operation

Callerrunspolicy: As long as the thread pool is not closed, the policy runs the currently discarded task directly in the caller's thread.

Discardoldestpolicy: Discards the oldest request and attempts to submit the current task again.

Discardpolicy: Discards a task that cannot be processed and does not give any processing.

If you need a custom deny policy, you can implement the Rejecyedexecutionhandler interface

See a bounded queue demo:

Can put the following pool one by one to run, in the run of the first 4 times, are executed each, a execution, the remaining 3 in the queue and then execute, run the first 5, the fifth and the second run simultaneously, because there are two threads, added 6th, the queue is full, Then there are no extra threads, only a deny policy to take a look at the results of the operation:

The following is a demo of an unbounded queue:

Promised to look at the results, the result is 5 tasks 5 tasks together, the use of unbounded queue when there is a limit, that is maxsize no meaning, just according to Coresize to create a thread, and then one by one to execute.

The following look at the bounded queue, is to change the Linkedblockingdeque to Arrayblockingqueue, run, you will find that he runs the result is 10 10 executed together, he is to perform 5 tasks, and then put 10 tasks in the queue, There are 5 tasks left and will be judged based on the second parameter. Discovery can be created, it continues to create 5 threads, and then the task is executed with 10 task 10 tasks.

Here's a look at the rejection policy:

The JDK custom rejection policy does not generally meet our needs, so we need to customize the rejection policy ourselves, the rejection policy needs to implement the Rejecyedexecutionhandler interface. Let's look at a simulated demo:

Then UseThreadPoolExecutor1 the code inside to modify it a little bit:

Look at the print results:

This rejection strategy is generally applied in high concurrency situations, and the system resources are saturated. A deny policy is generally just a log, and then when it is not peak time, it parses the log and handles the previously encountered rejection policy.

Custom thread pool usage details

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: 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.