2

Hi all, will daemon thread stop working when the enclosing it thread will finish? Or daemon thread will stop when the "main" thread will finish?

I tested this example on jre6 and result was daemon thread stopped working when the enclosing it thread finished. Notice that java docs said that daemon threads are killed when no other application threads remain. And it's not said that daemon threads are killed when parent non-daemon thread remains.

Please give me answers. Please send me any material about this question. Sorry for my English.

public class Main {
    public static void main(String[] args) {
        Thread simple = new Thread(new SimpleTask());
        simple.start();
    }
}

class SimpleTask implements Runnable {
    public void run() {
        try {
            Thread daemon = new Thread(new DaemonTask());
            daemon.setDaemon(true);
            daemon.start();
            Thread.sleep(5000);
        } catch (InterruptedException e) {}
    };
}

class DaemonTask implements Runnable {
    public void run() {
        int i = 0;
        while (true) {
            try {
                System.out.println("a" + (i++));
                Thread.sleep(500);
            } catch (InterruptedException e) {}
        }
    }
}
Vaandu
  • 4,857
  • 12
  • 49
  • 75
Tim
  • 440
  • 1
  • 5
  • 12

4 Answers4

11

will daemon thread stop working when the enclosing it thread will finish?

There's no such concept as an "enclosing thread" in Java. There are thread groups but they're rarely used.

Daemon threads are simply threads which don't stop the JVM from terminating. When there aren't any non-daemon threads left, the JVM will terminate. If there are still some non-daemon threads executing, the JVM will keep going, including any daemon threads - whether or not the threads that started those daemon threads have finished.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Spot on! The key is "When there aren't any non-daemon threads left, the JVM will terminate" – Manish Dec 30 '11 at 09:24
  • What Jon Skeet said is correct. In your example, the daemon thread is stopped when your SimpleTask is terminated. The reason is that your main thread is stopped before SimpleTask is terminated. So when SimpleTask is terminated that is the last non-demon thread and hence your daemon thread is also stopped – Pragalathan M Dec 30 '11 at 09:26
3

Hi all, will daemon thread stop working when the enclosing it thread will finish? Or daemon thread will stop when the "main" thread will finish?

A daemon thread will be stopped by the JVM when the main execution thread and all the user threads terminated their execution. Then, your daemon thread is strictly dependent from the execution of the user threads and the main thread of your program.

Instead, the JVM will shutdown your program until all the user threads have been terminated.

As recap, the user thread is a thread that keeps a program from quitting, because, even if the main thread of your program is terminated, the JVM doesn't stop your program until all the user threads have completed the requested job. Only when all the user threads have terminated, the JVM can shutdown the program.

Then, a daemon thread is a thread that doesn't keep your program from quitting. For other info, check this old question of SO.

Check the Thread API. The documentation for setDaemon() method reports below:

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 thread.

Or the Runtime API:

The Java virtual machine shuts down in response to two kinds of events:

1) The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or

2) The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
1

Since the main thread ends as soon as the simple thread is started, the simple thread is the last application thread to finish, so the daemon thread ends when this thread ends.

The fact that the simple thread is the one that started the daemon thread has nothing to do with that.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

The Daemon Thread stopped working due to the fact that there weren't any non-daemon threads left and NOT because the parent thread completed.

Manish
  • 3,913
  • 2
  • 29
  • 45