I know that Deamon threads background threads. We can create our own daemon thread by calling setDaemon(true)
.
My question is: why and when do we need to create our thread as daemon thread?
See Question&Answers more detail:osI know that Deamon threads background threads. We can create our own daemon thread by calling setDaemon(true)
.
My question is: why and when do we need to create our thread as daemon thread?
See Question&Answers more detail:osThe JVM exits when all the running threads are daemon threads. So imagine you're writing a simple game where your main method loops until you decide to quit. And imagine that at the start of the game, you start a thread that will endlessly poll some website to trigger alerts. You would like the JVM to exit when you decide to end the game. You don't want the endless polling to prevent the game from ending. So you make this polling thread a daemon thread.