search

Home  >  Q&A  >  body text

java - The main thread exits without the child thread ending

A bug occurred when running a program today, that is, a sub-thread task executed by the main function was launched before it was completed. However, after adding sleep() to the main program, the sub-thread will execute normally and be launched (but it will not work if the sleep time is too short). The debug sub-thread is mainly stuck on the line of code that performs database operations (maybe because it is more time-consuming, and the main thread has no time to wait for you to finish...).


##

typechotypecho2718 days ago1103

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-06-12 09:23:01

    Check whether your child thread is set setDaemon(true), Jvm will exit in the following situations:

    1. When all running threads are daemon threads

    2. or when no non-daemon thread is running

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-12 09:23:01

    There is no parent-child relationship between threads. If you need to wait, you can consider the join() method. Reference:
    /a/11...

    reply
    0
  • Cancelreply