Dangling Threads in Java

穿精又带淫゛_ 提交于 2020-01-23 02:12:51

问题


What happens to dangling threads in Java?

Like if I create an application and it spawns multiple threads. And one of the threads does not finish and the main program finishes before that. What will happen to this dangling thread? Will it stay in the thread pool infinitely or JVM will kill the thread after a threshold time period???


回答1:


It depends on if the thread has been marked as "daemon" or not. Daemon threads will be killed when the JVM exits. If there are any threads that are not daemon then the JVM will not exit at all. It will wait for those threads to finish first.

By default, threads take the daemon status of their parent thread. The main thread has daemon set false so any threads forked by it will also be false. You can set the daemon flag to true before the thread starts with this:

Thread thread = new Thread(...);
thread.setDaemon(true);
thread.start();


来源:https://stackoverflow.com/questions/10029076/dangling-threads-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!