Program does not terminate immediately when all ExecutorService tasks are done

后端 未结 6 1121
一整个雨季
一整个雨季 2020-12-29 21:22

I put a bunch of runnable objects into an ExecutorService:

// simplified content of main method
ExecutorService threadPool = Executors.newCachedThreadPool();         


        
6条回答
  •  忘掉有多难
    2020-12-29 22:03

    It is due to combination keepAliveTime=60L, timeunit=TimeUnit.SECONDS and corePoolSize=0*: when thread completes task, it does not terminate immediately, it may** wait during keepAliveTime for a new task.

     public static ExecutorService newCachedThreadPool() {
            return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                          60L, TimeUnit.SECONDS,
                                          new SynchronousQueue());
        }
    

    *if core poolSize != 0 see method allowCoreThreadTimeOut() of ThreadPoolExecutor

    **waiting depends on combination of current quantity of running threads in pool, corePoolSize and maximumPoolSize

提交回复
热议问题