|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| what is daemon??? |
|
![]() EkEk Member since: 8/28/2003 From: Jakarta, Indonesia |
||||
|
|
||||
| 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() |
||||
|
||||
![]() Shadowdancer Member since: 6/25/2000 |
||||
|
|
||||
| Look here |
||||
|
||||
![]() Aldacron Member since: 10/17/1999 From: Seoul, Korea, Republic of |
||||
|
|
||||
| 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 Member since: 11/29/2001 From: Ottawa, Canada |
||||
|
|
||||
quote: 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] |
||||
|
||||
![]() Aldacron Member since: 10/17/1999 From: Seoul, Korea, Republic of |
||||
|
|
||||
quote: 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. |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
quote: Nope. Maybe you're thinking of process spawning in Unix systems. Maybe you've never had multiple user threads with daemon threads simultaneously. Maybe you're just completely confused. But regardless of why, you're wrong. Take a look at this:
public class Test
{
public static void main(String [] args)
{
// bad style, used only to make the example shorter.
Thread d = new Thread()
{
public void run()
{
int i = 0;
while (true)
{
System.out.println(i++);
try { Thread.sleep(500); }
catch (InterruptedException e) {}
}
}
};
d.setDaemon(true);
d.start();
Thread nd = new Thread()
{
public void run()
{
try { Thread.sleep(5000); }
catch (InterruptedException e) {}
}
};
nd.start();
System.out.println("Main thread exiting");
}
}
|
||||
|
||||
![]() EkEk Member since: 8/28/2003 From: Jakarta, Indonesia |
||||
|
|
||||
| all: thanks guys for the answers. Anonymous: i have tried your code, it's give me the picture. quote: 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 Member since: 11/29/2001 From: Ottawa, Canada |
||||
|
|
||||
quote: 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 |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|