What happens to the ThreadPoolExecutor when thread dies in Java

前端 未结 2 1291
礼貌的吻别
礼貌的吻别 2021-01-14 12:12

I have created a thread which in turn creates a ThreadPoolExecutor and submits some long running tasks to it. At some point, the original thread dies due to unh

2条回答
  •  Happy的楠姐
    2021-01-14 12:42

    Threads are so called GC roots. This means among other things, that a running (or an unstarted) thread can't be collected. It also means that objects that are being referenced from those threads can't be collected, which is why you can do things like new Thread(new MyRunnable()).start(), or have threadpools running without you having any reference to them.

    If the thread is a daemon, it can stop automatically if all other non-daemon threads have stopped. You can have threadpools with daemon threads, but the best thing is to make sure that things are cleaned up properly (i.e. an exception shouldn't kill the thread that's supposed to eventually stop and cleanup the threadpool).

提交回复
热议问题