Java concurrent programming eighth chapter thread pool use reading notes

Source: Internet
Author: User


I. Implicit decoupling between tasks and operational strategies some types of tasks need to clearly specify a running policy, including:
. Dependency tasks. Dependencies have constraints on running policies and need to be aware of active issues. Ask the thread pool to be large enough to ensure that the task is put in.
. A task that uses the thread closure mechanism. Serial operation is required.
. A task that is sensitive to response time.
. tasks that use threadlocal.
1. The thread starvation deadlock thread pool assumes that all the threads running the task are blocked by waiting for other tasks that are still in the work queue, which is called thread starvation deadlock.
2. Tasks that take longer to execute Java provides a time-limited version number and an infinite-time version number. such as Thread.Join, Blockingqueue.put, cutdownlatch.await, Selector.select
Setting the thread pool size to correctly set the size of the thread pool, you must estimate the ratio of the task's wait time to the calculated time. Such estimates do not need to be very precise and can be obtained through a number of analysis or monitoring tools.

Formula definition: int n_cpus = Runtime.getruntime (). Availableprocessors (); The CPU is not the only resource that affects the thread pool size. It also contains memory, file handles, socket handles, database connections, and so on.

Calculate the demand for this resource for each task. It then divides the available total of the resource by the demand for each task, resulting in the upper limit of the thread pool size.


Third, configuration Threadpoolexecutor
Description of the constructor in the API documentation:
Public threadpoolexecutor (int corepoolsize, int maximumpoolsize, long ke                          Epalivetime, Timeunit unit, blockingqueue<runnable> WorkQueue, Rejectedexecutionhandler handler) creates a new threadpoolexecutor with the given initial number of parameters and the default thread factory.

1. Thread pool creation and destruction can view executors several methods source code to aid in understanding the configuration of threadpoolexecutor parameters.


2. Manage Queue Task Threadpoolexecutor agreed to provide a blockingqueue to save the task waiting to run.

There are 3 main queue methods: Unbounded queue, bounded queue, synchronous handoff.

Java.util.concurrent.BlockingQueue all known implementation classes:
Arrayblockingqueue, Delayqueue, Linkedblockingdeque, Linkedblockingqueue, Priorityblockingqueue, SynchronousQueue

3. Saturation policy The bounded queue has been filled up or the executor has to be considered when submitting a task to a closed one.
Threadpoolexecutor.setrejectedexecutionhandler
public void Setrejectedexecutionhandler (Rejectedexecutionhandler handler) sets a new handler for the task that is not running.



Java.util.concurrent.RejectedExecutionHandler all known implementation classes:
Threadpoolexecutor.abortpolicy, Threadpoolexecutor.callerrunspolicy, Threadpoolexecutor.discardoldestpolicy, Threadpoolexecutor.discardpolicy


AbortPolicy is the default saturation policy--to be populated
4. Factory method
5. After calling the constructor, customize the Threadpoolexecutor after the thread pool is created. It is still possible to change the number of parameters passed in during construction by means of the threadpoolexecutor provided.
Iv. Expansion of Threadpoolexecutor
The parallelization of recursive algorithm is not used temporarily




Java concurrent programming eighth chapter thread pool use reading notes

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.