Difference between a daemon thread and a low priority thread

前端 未结 4 1122
忘了有多久
忘了有多久 2020-12-06 11:56

Recently I was asked a question:

We\'ve got the setPriority() method to set a thread for low priority. Then why do we need a daemon thr

4条回答
  •  一生所求
    2020-12-06 12:44

    If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In order for an application to continue running, it must always have at least one live user thread. In all other respects the Java runtime treats daemon threads and user threads in exactly the same manner.

    except that in daemon thread .. when JVM terminate abruptly then finally blocks are not executed, stacks are not unwound – JVM just exits. Due to this reason daemon threads should be used sparingly and it is dangerous to use them for tasks that might perform any sort of I/O.

提交回复
热议问题