java 中创建线程池的问题。

2025-05-19 11:19:03
推荐回答(2个)
回答(1):

看下源码你就明白了

   /**
    * 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 {@link ThreadPoolExecutor} constructors.
    *
    * @return the newly created thread pool
    */
   public static ExecutorService newCachedThreadPool() {
       return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                     60L, TimeUnit.SECONDS,
                                     new SynchronousQueue());
   }


这个只是初始化的数据,具体线程池了有多少线程是active的,要看你怎么用,像你上面跑的代码,线程池里就只有一个线程在跑。

回答(2):

线程池中的线程,也会随着线程结束而消亡的。

尝试debug出来状态吧