Why does a thread outlive the main method in Java?

前端 未结 6 2029
小蘑菇
小蘑菇 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:30

    Because that's how it works. The program exits when System.exit() is called, or when the last non-daemon thread stops running.

    And it makes sense. Without this rule, every Java program consisting in just spawning a GUI, for example, would have to wait() infinitely to avoid the program from exiting immediately.

提交回复
热议问题