Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why need use non-daemon threads in java?

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.

like image 363
user710818 Avatar asked Dec 03 '22 05:12

user710818


1 Answers

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.

like image 96
Tomasz Nurkiewicz Avatar answered Dec 05 '22 17:12

Tomasz Nurkiewicz