Why doesn't this thread pool get garbage collected?

前端 未结 6 582
长情又很酷
长情又很酷 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 05:00

    Once executorService is out of scope, it should get collected and finalized.

    Not really - once it is out of scope, it could get collected and finalized. There are no guarantees made in the VM spec about when objects are finalized, or even if they are finalized:

    The Java programming language does not specify how soon a finalizer will be invoked, except to say that it will happen before the storage for the object is reused.

提交回复
热议问题