| Threads can be one of two types: a user thread or a daemon thread. So what is a daemon ? Well, Webster's Dictionary says it is " an attendant spirit," or " an inferior deity." In a sense, Webster's is right in both cases with respect to daemon threads. Daemon threads usually perform a valuable service for the application by doing some task that the rest of the application doesn't really want to do. This may be personifying an application, but daemon threads usually end up performing tasks that, although important, are somewhat less critical to the application. Daemon threads are also inferior to normal user threads. This is because as long as a user thread is running, the JVM will not stop an application. If there are only daemon threads running in an application, the JVM will stop the application. Two methods in java.lang.Thread deal with the daemonic state assigned to a thread:
The first method, isDaemon, is used to test the state of a particular thread. Occasionally, this is useful to an object running as a thread so that it can determine whether it is running as a daemon or a regular thread. isDaemon returns true if the thread is a daemon, and false otherwise . The second method, setDaemon(boolean), is used to change the daemonic state of the thread. To make a thread a daemon, you indicate this by setting the input value to true. To change it back to a user thread, you set the boolean value to false. Here's an example of using a daemon thread. Listing 11.18 is a class that implements the Runnable interface. In the run method, it has an infinite loop that prints out a line of text each time through the loop. This would normally be a bad idea, but because a thread is being started up as a daemon, the application should not run forever because there are only daemon threads running. The JVM should detect that there are only daemon threads running and stop the entire application. Listing 11.18 Source Code for MyDaemon.java public class MyDaemon implements Runnable { // Default Constructor public MyDaemon() { super(); } // Implement the required method from the Runnable interface public void run() { // Go into an infinite loop. This is normally not a good idea while(true) { // Print this out each time through the loop System.out.println("Still Running"); } } } Listing 11.19 is the main class used to start the daemon thread running. Listing 11.19 Source Code for DaemonThreadMain.java public class DaemonThreadMain { public DaemonThreadMain() { } // Main method that starts it all public static void main(String[] args) { // Create a new thread using the MyDaemon class as the runnable target Thread thread = new Thread(new MyDaemon(), "My Daemon Thread"); // Set the thread up as a daemon thread thread.setDaemon(true); thread.start(); } } Here's the output from the example: C:\jdk1.3se_book\classes>java DaemonThreadMain Still Running Still Running Still Running Still Running Still Running Still Running Still Running Still Running Still Running Still Running Still Running C:\jdk1.3se_book\classes> Notice that even though there was an infinite loop in the run method, the application exited. It did make it several times through the loop, but exited when the JVM realized it was only daemon threads running. |