Original Post
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()
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.
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.
quote:
The reason for the distinction is that the VM will only shut down when no user threads are alive.
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??
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");
}
}
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more