Daemon Threads
Home ] Up ] What is a Thread? ] Thread States ] [ Daemon Threads ] Thread Operations ] Some Useful Methods ] Deprecated Methods ]

 

 

Some threads are intended to be "background" threads, providing services to other threads. These threads are called daemon threads

UNIX has a similar concept for processes, where certain operating system services are provided by daemon processes.  (For some reason, it has become traditional to use the archaic spelling for demon.)  Microsoft Windows uses Windows services for similar operations.

When a Java thread is created, it is a non-daemon thread. It may be changed (or change itself) to a daemon thread by calling its setDaemon() method. (There is also a matching isDaemon() method.)

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class), and one daemon thread -- the garbage collection thread. The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called (System.exit() calls this method) and the security manager has permitted the exit operation to take place,

or:

  • All threads that are not daemon threads have died, either by returning from the call to the run method or by their stop (deprecated!)method being called.
 
The page was last updated February 19, 2008