Why doesn't this thread pool get garbage collected?

前端 未结 6 600
长情又很酷
长情又很酷 2020-12-08 04:36

In this code example, the ExecutorService is used one and allowed to go out of scope.

public static void main(String[] args)
{
    ExecutorService executorSe         


        
6条回答
  •  没有蜡笔的小新
    2020-12-08 04:56

    If you want the threads to be finalized when the Executor service is out of scope you should avoid, as mjt suggested, the use of

        ExecutorService executorService = Executors.newFixedThreadPool(3);`
    

    and use for example:

    ExecutorService executorService = new ThreadPoolExecutor(0, 3, 10, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
    

提交回复
热议问题