Chapter 1 selection of thread pool and thread count, selection of Thread Pool

Source: Internet
Author: User

Chapter 1 selection of thread pool and thread count, selection of Thread Pool

Note: This chapter mainly refers to "large-scale distributed Java applications: basic and practice" and "large-scale website technical architecture: Core Principles and case analysis".

1. Two Selected angles

  • High Performance: Submit the tasks submitted to the thread pool to the thread for processing (premise: the number of threads is smaller than the maximum number of threads ).
  • Buffer execution: The tasks to be submitted to the thread pool should be executed by the Core Thread (corePoolSize) as much as possible.

2. High Performance

  • Queue: SynchronousQueue
  • Maximum number of threads: generally set to Integer. MAX_VALUE (maximum Integer value) to prevent rejection of tasks.
  • Typical Case: newCachedThreadPool
  • It is especially suitable for executing short-lived tasks.

Note:

 

  • Set the idle expiration time and keepAliveTime to avoid high resource consumption.
  • For a large number of time-consuming tasks, it is easy to cause a rapid increase in the number of threads. In this case, it is necessary to determine whether this type of thread pool is suitable.

3. Buffer execution

  • Queue: LinkedBlockingQueue and ArrayBlockingQueue
  • Typical Case: newFixedThreadPool (int threadSize)

Note:

  • When this type of thread pool is used, it is best to use javasblockingqueue (unbounded Queue). However, when a large number of concurrent tasks flood in, leading to insufficient processing of core threads, the queue elements will increase significantly and may report memory overflow.
  • Of course, in this case, if ArrayBlockingQueue is used properly, you can reject some tasks without reporting memory overflow.

4. determine the number of threads

  • Formula: Number of start threads = [task execution time/(task execution time-IO wait time)] * Number of CPU Cores

Note:

  • If most tasks are CPU computing tasks, the number of startup threads = the number of CPU Cores
  • If most tasks need to wait for disk operations and network response, you can estimate the number of startup threads by reference to the formula. Of course,> Number of CPU Cores

 

Summary:

Generally, thread pools are used in the following order (the latter is considered only when the former does not meet the needs of the scenario ):

NewCachedThreadPool --> newFixedThreadPool (int threadSize) --> ThreadPoolExecutor

  • NewCachedThreadPool does not need to specify any parameters
  • NewFixedThreadPool specifies the number of thread pools (number of core threads = maximum number of threads)
  • ThreadPoolExecutor needs to specify the number of core threads, maximum number of threads, idle timeout time, queue, queue capacity, and even the reject policy and thread factory.

For the number of cores of newFixedThreadPool and ThreadPoolExecutor, you can use the formula given above to estimate the number of cores.

Related Article

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.