- Mar 22, 2002
User Threads and Daemon Threads
Java regards threads as user threads or daemon threads. User threads are the default. When the main thread terminates, the JVM checks to see if any other user thread is running. If so, the JVM does not terminate the application. On the other hand, if the JVM detects only daemon threads, the application terminates.
Daemon threads are designed as low-level background threads that perform useful work. However, it is not essential that they be allowed to complete before an application terminates. One example of a daemon thread is the garbage collector thread.
To create a daemon thread, call Thread's setDaemon() method with a Boolean true argument value. That method must be called before Thread's start() method. After start() is called, subsequent setDaemon() calls result in IllegalThreadStateException objects being thrown. To determine whether a Thread object is associated with a daemon thread, call Thread's isDaemon() method.