Program does not terminate immediately when all ExecutorService tasks are done

后端 未结 6 1080
一整个雨季
一整个雨季 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 21:50

    From the javadoc for Executors.newCachedThreadPool():

    Threads that have not been used for sixty seconds are terminated and removed from the cache.

    It is usually a good idea to call shutdown() on an ExecutorService if you know that no new tasks will be submitted to it. Then all tasks in the queue will complete, but the service will then shut down immediately.

    (Alternately, if you don't care if all the tasks complete - for example, if they are handling background calculations that are irrelevant once your main UI is gone - then you can create a ThreadFactory that sets all the threads in that pool to be daemon.)

提交回复
热议问题