Executors, ThreadPoolExecutor thread pool use

Executors: JDK tools provided to the thread, the thread pool static methods to build service ExecutorService, is ThreadPoolExecutor, use the default thread pool configuration parameters.

    Recommendation: For large users, high concurrency, easy to control the project, does not recommend the use of Executors to create a thread pool object.

      For easy to control and not high number of concurrent projects, consider Executors.

ThreadPoolExecutor: thread pool object ExecutorService implement the interface, you can customize the core number of threads in the thread pool is defined, the maximum number of threads, idle time, buffer queues.

    Recommendation: large users, high concurrency, easy to control the project, the proposed configuration of the physical server needs time-consuming task, complicated by the amount that may be present, custom configuration ThreadPoolExecutor thread information.

      General project encapsulates ThreadPoolExecutor tools, because of the need to consider novices sometimes indiscriminate use, but also easy to unified management and flexible.

 

First, start three strategies under Executors create a thread pool

  1. Create a borderless cache thread pool: Executors.newCachedThreadPool ()

    Creating an infinity pool cache unbounded thread: the thread pool does not limit the maximum number of threads, and thread have long survived idle.

    Then look JDK Executors.newCachedThreadPool () Source:

    

    Source can be seen, this method creates a thread pool: 0 Number of kernel threads, the maximum number of threads Integer.MAX_VALUE (can be understood as no upper limit), the thread is idle survive long 60 units TimeUnit.SECONDS seconds buffer queue is SynchronousQueue the thread pool.

    Because the maximum number of threads is no upper limit, so large users, high concurrent projects must use strict configuration.

    About SynchronousQueue buffer queue buffer number is 1, but each time have been removed to create a thread pool, so the buffer couplet always isEmpty = true. Specific details of the first Baidu, the follow-up time for me to explain.

  2. Create a bounded thread pool: Executors.newFixedThreadPool (200)

    There are boundaries to create a thread pool: Create a maximum number of threads have a thread pool limit.

    Then look JDK Executors.newFixedThreadPool (200) Source:

    

 

 

    Source can be seen, this method creates a thread pool: the core number of threads nThreads (200), the maximum number of threads nThreads (200), long thread idle survival 0 units TimeUnit.SECONDS seconds buffer queue thread pool is LinkedBlockingQueue .

    LinkedBlockingQueue length of not defined here, that is to say the capacity of the buffer queue is no upper limit, large users, high stringency must be used concurrently project configuration.

Guess you like

Origin www.cnblogs.com/zwcry/p/12050383.html