Home » Community » Forums » Java Development » what is daemon???
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 what is daemon???
Post New Topic  Post Reply 
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()

 User Rating: 1016   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Look here

 User Rating: 1137   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1462   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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]

 User Rating: 1279   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1462   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

quote:
Original post by Aldacron
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.


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");
    }
}


 User Rating: 1015    Report this Post to a Moderator | Link

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]

 User Rating: 1016   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1279   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: