Comparison of several thread pools in Java Executorservice

Source: Internet
Author: User

One: Newcachedthreadpool

(1) Cache-type pool, first look at the pool there are no previously established threads, if there is, reuse, if not, set up a new thread to join the pool;

(2) buffer pool, usually used to perform some short-lived asynchronous tasks, so some connection-oriented daemon-type server is not used much;

(3) The thread that can reuse, must be timeout idle in the pool line, the default timeout is 60s, more than this idle time, the thread instance will be terminated and removed from the pool.

(4) Note that the thread that is put into the cachedthreadpool does not have to worry about its end, exceeding timeout inactivity, which is automatically terminated

Two: Newfixedthreadpool

(1) Newfixedthreadpool and Cachethreadpool are similar, but also can reuse use, but can not build new threads at any time

(2) Its uniqueness: At any point in time, there can be at most a fixed number of active threads, at this point, if there are new threads to be established, can only be placed in another queue wait until a thread in the current thread termination is directly removed from the pool

(3) Unlike Cachethreadpool, Fixedthreadpool has no idle mechanism (possibly, but since the document is not mentioned, it must be very long, similar to the TCP or UDP idle mechanism that relies on the upper layer), So Fixedthreadpool most for some very stable and fixed regular concurrent threads, more for the server

(4) from the source code of the method, the cache pool and the fixed pool call the same underlying pool, except that the parameters are different:
Fixed pool thread count, and is 0 seconds idle (no idle)
Cache Pool Threads Support 0-integer.max_value (obviously not considering the host's resource tolerance), 60 seconds idle

Three: Scheduledthreadpool

(1) Scheduling thread pool

(2) The thread in this pool can be executed by schedule, or cycle execution

Four: Singlethreadexecutor

(1) Single thread, only one thread can be in any time pool

(2) using the same underlying pool as the cache pool and the fixed pool, but the number of threads is 1-1, 0 seconds idle (no idle)

Several thread pool comparisons for Java Executorservice

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.