18

I cannot understand the usage and the purpose of daemon threads.

What are they for? How can I use them? Also, I tried to create daemons but I couldn't.

class Evil implements Runnable {
    public static void main(String[] arg) throws Exception {
        Thread t = new Thread(new Evil());
        t.start();
        Thread.sleep(1000);
        t.setDaemon(true);//no success, error!
    }

    public void run() {
        try {
            Thread.sleep(1000);
            System.out.println("How would it be Evil!?");
            Thread.sleep(1000);
        } catch (Exception e) {
        }
    }
}

This is what I attempted so far, but it's not working properly.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • 7
    You can't make a `Thread` daemon once it's being started – MadProgrammer Oct 17 '13 at 07:50
  • Remember that daemons are threads that will live for themself, if you create a daemon is because you will not control them in their life-cycle – RamonBoza Oct 17 '13 at 07:52
  • 10
    Note that the [daemon](http://en.wikipedia.org/wiki/Daemon_%28computing%29) term of computer science comes from [Maxwell's demon](http://en.wikipedia.org/wiki/Maxwell%27s_demon) which itself refers to the greek interpretation of the word, which does *not* have any bias toward evil(or good); it's neutral. Hence naming the `Runnable` class `Evil()` isn't really appropriate. – Bakuriu Oct 17 '13 at 11:41
  • thanks dude, good point. –  Oct 17 '13 at 19:27
  • It' all there in the JavaDoc. – Raedwald Oct 18 '13 at 07:19

4 Answers4

56

First you need to set a thread as daemon just before you start it, so the first thing would be like this:

 Thread t = new Thread(new Evil());
 t.setDaemon(true);//success is here now
 t.start();
 Thread.sleep(1000);

Daemon threads are like normal (user) threads, but there is a big difference. The JVM kills (halt) the application when there is no user thread exist (alive), in other word if you have 1 user thread (main thread for example) and 1000 daemon threads, here the JVM sees one thread in your application, and it kills the application just after that main thread finishes its job.

These threads are good for handling or doing some business logic in the background till other user threads alive, and beware about changing anything withing daemon thread, because there is no any signal before halting a thread by JVM.

So in you case, where daemon thread waits for 1 second and say something and again sleep for 1 second, because this is daemon, and main threads is no more after 1 second, then daemon thread never reaches the second sleep line.

This (diagram) may help you too. from arashmd.blogspot.com

Chin
  • 19,717
  • 37
  • 107
  • 164
  • 10
    voted up for your diagram :) – RamonBoza Oct 17 '13 at 07:53
  • now after I set the daemon before I start it, there is no error, but I still cannot understand the usage and how do you say the evil should not reach the second sleep?! –  Oct 17 '13 at 08:03
  • just check the diagram again! the daemon threads don't specify the application life time, in other word they live for themselves, in other word JVM doesn't see the daemon thread as applciation execution(life-time), and in you code you have two thread, a daemon one, and main, and if you look closely, main thread is no more after 1 second, while daemon thread may need 2 seconds. the usage would be for doing something in back ground(listening for something, handle events, counting time), but beware, because they are killed silently. –  Oct 17 '13 at 08:08
  • so if you don't mind I ask a stupid question, so this is madness to make all threads as daemon(my tutor said making all threads daemon makes execution faster)? –  Oct 17 '13 at 08:11
  • 1
    Wow, Cool! yes this is madness! so how do you manage the life-cycle of your application?! this may make sense when you want to kill the application after period of time, so make all daemon(while this would be managed with better way) and a user thread which counts. I haven't heard about the daemon threads make execution faster!? how really? –  Oct 17 '13 at 08:14
10

The javadoc for Thread.setDaemon(boolean) says:

Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

This method must be called before the thread is started.

A good example for a deamon thread is a timer.

It makes no sense that a timer fires one more time if there are no user threads anymore.

Community
  • 1
  • 1
René Link
  • 48,224
  • 13
  • 108
  • 140
5
  1. Daemon threads aren't evil (although technically they could do evil things).
  2. You can't make a thread daemon after it has been started.
  3. You would use a daemon thread as a background thread that mustn't/doesn't need to prevent the program from closing.
Kayaman
  • 72,141
  • 5
  • 83
  • 121
5

Daemon status must be set before starting the thread

A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120