Ali explained about the development of the manual to prevent create a thread pool with the Executor

Executors is a utility class that can be called directly using Executors thread pool method JDK has provided us.


Executors.newCachedThreadPool(); // 创建可缓存的线程池

Executors.newFixedThreadPool(1); // 创建固定大小的线程池

Executors.newSingleThreadExecutor(); //  创建单线程池

- newCachedThreadPool is a cached thread pool, when a thread pool thread number is greater than the number of tasks to be performed, resources will be recovered off the thread after 60 seconds.

 When the task to increase the amount corresponding to the thread pool will create a thread to perform the task.

newCachedThreadPool source

- newFixedThreadPool is a fixed-size thread pool, incoming parameter is used to set the maximum number of threads, the threads are executed at this time if the state will be placed in the task to the task queue.

newFixedThreadPool source

- newSingleThreadExecutor is a single-threaded thread pool, this thread pool threads can guarantee submitted in accordance with the order to complete.

newSingleThreadExecutor source

Through the above source code analysis, we found newFixedThreadPool and newSingleThreadExecutor methods they have used LinkedBlockingQueue task queue, the default size LinkedBlockingQueue is Integer.MAX_VALUE. The newCachedThreadPool defined in the thread pool size Integer.MAX_VALUE.

So the reason Ali prohibit the use of Executors create a thread pool is a request queue length FixedThreadPool and SingleThreadPool for Integer.MAX_VALUE, may accumulate a large number of requests, causing OOM.

CachedThreadPool allowed number of threads to create Integer.MAX_VALUE, may create a large number of threads, resulting OOM.

Original: https://segmentfault.com/a/1190000021657959

Published 740 original articles · won praise 65 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_41723615/article/details/104365363