Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] what is daemon???

Started by EkEk Feb 19, 2004 at 5:54 AM 6 replies 2k views
Original Post
EkEk
EkEk
maybe this is the most stupid question in the world. But can anyone explain to me what the meaning of Daemon??? i see this daemon in Thread.setDaemon()
Aldacron
Aldacron
More specifically, when you create a new thread in Java and flag it as a daemon by passing true to setDaemon, you are indicating to the VM that the thread should be terminated when thread which spawned it has terminated.
CaptainJester
CaptainJester
quote:
Original post by Aldacron
More specifically, when you create a new thread in Java and flag it as a daemon by passing true to setDaemon, you are indicating to the VM that the thread should be terminated when thread which spawned it has terminated.


That is not correct. There are 2 types of threads in Java. User threads and daemon threads. Daemon threads are basically threads that are there to provide a service to user threads. As an example, the garbage collecter is a daemon thread. The reason for the distinction is that the VM will only shut down when no user threads are alive.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

[edited by - CaptainJester on February 19, 2004 8:04:34 AM]
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Aldacron
Aldacron
quote:
Original post by CaptainJester
That is not correct. There are 2 types of threads in Java. User threads and daemon threads. Daemon threads are basically threads that are there to provide a service to user threads. As an example, the garbage collecter is a daemon thread. The reason for the distinction is that the VM will only shut down when no user threads are alive.



Sorry, but it *is* correct. Yes, there are user threads and daemon threads. But when you create a new thread and call setDaemon(true), the new daemon becomes subordinate to the thread that created it . When the creating thread ends, the daemon dies with it.

So if you spawn any daemons off of the main thread, they will be killed when the app exits, since the main thread will be terminated.
EkEk
EkEk
all:
thanks guys for the answers.
Anonymous:
i have tried your code, it's give me the picture.

quote:

The reason for the distinction is that the VM will only shut down when no user threads are alive.


So this Daemon Thread will be killed after user threads killed??
or is the VM will shutdown without give a damn about this Daemon Thread killed or still alive??

EDIT: Changing the question

[edited by - EkEk on February 20, 2004 2:10:26 AM]

[edited by - EkEk on February 20, 2004 2:13:11 AM]
CaptainJester
CaptainJester
quote:
Original post by EkEk
So this Daemon Thread will be killed after user threads killed??
or is the VM will shutdown without give a damn about this Daemon Thread killed or still alive??



The VM will only continue to run as long as a user thread is alive. As soon as there are no user threads left, the VM will shut down any daemon threads and then exit.

Here''s another good example. The thread that start''s both threads exits, but they both continue to run. When the second user thread exits, the daemon thread is killed.

public class ThreadTest extends Thread {
boolean daemon;
boolean alive = true;
int x = 0;

public ThreadTest(boolean daemon) {
this.daemon = daemon;
}
public void run() {
while(alive) {
if(daemon) {
System.out.println("daemon thread running");
}
else {
System.out.println("user thread running");
}
try {
Thread.sleep(100);
x++;
if(!daemon && x >= 100) {
alive = false;
}
}
catch(Exception e) {
e.printStackTrace();
}
}
if(daemon) {
System.out.println("daemon thread exiting");
}
else {
System.out.println("user thread exiting");
}
}

public static void main(String args[]) {
System.out.println("Main user thread started");
System.out.println("Starting daemon thread");
ThreadTest td = new ThreadTest(true);
td.setDaemon(true);
td.start();
System.out.println("Starting user thread");
ThreadTest tu = new ThreadTest(false);
tu.start();
System.out.println("main user thread exiting");
}
}




First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.