Why does a thread outlive the main method in Java?

前端 未结 6 2043
小蘑菇
小蘑菇 2020-12-08 14:19

I was teaching myself Java threading and I noticed something that confuses me a little. I made a class called engine implementing Runnable. The r

6条回答
  •  一个人的身影
    2020-12-08 14:24

    There are two types of threads, user and daemon. The process terminates when there are no more user threads. The main thread is always a user thread. The thread that you start is also a user thread, and therefore keeps the process alive for as long as it runs.

    Calling setDaemon(true) on your thread before you start it will make your process terminate (more or less) as soon are your main() function returns.

提交回复
热议问题