使用 Executors.newCachedThreadPool() 时在线程池中创建的线程数

Number of threads created in the thread pool when using Executors.newCachedThreadPool()

使用 Executors.newCachedThreadPool() 时,线程池中最初创建了多少个线程 Executors.newCachedThreadPool() Javadoc 没有指定任何数字是否有一个保证的数字,我们最初总是会得到像 10 或下面的 something.Docs :

newCachedThreadPool public static ExecutorService newCachedThreadPool() Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors. Returns: the newly created thread pool

答案是0

您可以在 source code that right after ThreadPoolExecutor creation there is no workers spawned 中找到。