Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to be a daemon thread?

Tags:

java

I'm fully aware of the impact of flagging a thread as a daemon thread on JVM exit (if all non-daemon threads exit, the JVM will exit.)

Are there other side-effects to setting a thread as a daemon?

(as a complete aside, which is really irrelevant except for semantics, is the meaning of a daemon thread not backwards in this case: shouldn't a daemon thread keep the JVM alive...for instance, if I wrote my own HTTP daemon and started it running, wouldn't I want the JVM to remain alive as long as that thread is alive? Or is there some other semantic for the term 'daemon'?)

like image 656
Jared Avatar asked May 18 '09 21:05

Jared


2 Answers

As you have it, it means that the thread is detached and will run as long as the JVM runs, unless you stop it explicitly. By spec, the JVM keeps running as long as any non-daemon thread is running.

Beyond that, it doesn't mean much of anything.

You could argue that this is backwards — after all, shutdown(8) kills the daemon processes — but the JVM should act like a well-behaved process otherwise, which means people should be able to expect it to terminate without having to be killed. So this spec is a design decision based on what the more common use case will be.

like image 71
Charlie Martin Avatar answered Oct 12 '22 20:10

Charlie Martin


A daemon thread is a service to other threads. Its only a daemon for the jvm itself not for the outside world. thus if only daemons excist they have no more use. If you want an http daemon you just write an httpserver with normal threads and invoke it as a daemon using the toolset of your os to run apps as a daemon.

To conlcude a daemon thread in jvm is not a daemon thread for the os. Daemons are usually infinite loops and you dont want your jvm to go on infinitely while all you have running are a few daemons thats wait for input(which is not given).

like image 44
youri Avatar answered Oct 12 '22 18:10

youri