It seems that daemon threads are always better - because they will be stopped by the VM after application main thread exits. Are there any other reasons to use non-daemon threads besides the cases when it is not possible to interrupt some operation ? Thanks.
When you are writing a server (e.g. a servlet container), all your main
has to do is to bootstrap and start HTTP listener threads, accepting threads, file system scanning threads, RMI threads, etc.
After bootstrap is done, main
is no longer needed as everything happens asynchronously. In this case all essential threads are non-daemon as they have to live past the main
method.
Even in Swing (desktop programming) the only requirement on main
is to initialize the main window (JFrame
). The rest happens in Swing listener threads (EDT) and various background threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With