When does the main thread stop in Java?

前端 未结 7 2106
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 15:06

I read this statement:

The main thread must be the last thread to finish execution. When the main thread stops, the program terminates.

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 15:47

    The first statementis not exact. The java program terminates when all non-daemon threads has been terminated or when System.exit() or Runtime.exit() is invoked.

    Thread is terminated when it exited its run() method. Main thread is special because you do not explicitly implement its run() method, you implement main() instead and the main() is called from run(). So, main thread is terminated when main() is terminated.

    But main thread is not neccessarely the last one.

提交回复
热议问题