aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes setDaemon method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "setDaemon method" Watch "setDaemon method" New topic
Author

setDaemon method

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
setDaemon method when set to true causes that thread to be a daemon thread instead of user.
i wrote a code and threads in there were set to daemon, and none of the thread ran. this is fine as threads were set to be daemon.
question is what are we trying to achieve then ? can someone explain ? also i know that if one of the user thread exists the other daemon threads would run. so what's the big deal. what are we trying to achieve.
and second what is the concept of "daemon thread group". what is the group here. daemon is set inidividually for a particular thread. where is the group here ?
i had posted this question and answer is 4 but can someone explain why 4 ?
---------------------------------------
A demon Thread group
1 has only demon threads.
2 can have non demon threads.
3 does not exist after all non demon threads in the group have finished executing.
4 does not exist after all the threads in the group have finished executing.
Answer :4
---------------------------------------
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by mark stone:
setDaemon method when set to true causes that thread to be a daemon thread instead of user.
i wrote a code and threads in there were set to daemon, and none of the thread ran. this is fine as threads were set to be daemon.
question is what are we trying to achieve then ? can someone explain ? also i know that if one of the user thread exists the other daemon threads would run. so what's the big deal. what are we trying to achieve.

A daemon thread is more often than not a "background" thread. The most common use for a daemon thread is to do something in the background (like a spell checker in a word processor, for example). The biggest distinction is that, when the application ends, you'd like this thread to automatically end, rather than stopping it explicitly. You could, for example, start a daemon thread to do spell checking as soon as someone starts your word processor application (a user thread). Then, until the user closes the word processor application, your daemon thread will continue to run. As soon as the user closes the word processor (your user thread), the daemon thread will stop without you having to do anything.
Daemon threads are often used in this way. I'm not sure what you mean by a daemon thread group, however. Perhaps someone else can help out with that part.
I hope that helps,
Corey


SCJP Tipline, etc.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by mark stone:
setDaemon method when set to true causes that thread to be a daemon thread instead of user.
i wrote a code and threads in there were set to daemon, and none of the thread ran. this is fine as threads were set to be daemon.
question is what are we trying to achieve then ? can someone explain ? also i know that if one of the user thread exists the other daemon threads would run. so what's the big deal. what are we trying to achieve.
and second what is the concept of "daemon thread group". what is the group here. daemon is set inidividually for a particular thread. where is the group here ?
i had posted this question and answer is 4 but can someone explain why 4 ?
---------------------------------------
A demon Thread group
1 has only demon threads.
2 can have non demon threads.
3 does not exist after all non demon threads in the group have finished executing.
4 does not exist after all the threads in the group have finished executing.
Answer :4
---------------------------------------

Continuation from Corey's comment......
Hi
A thread group can be a daemon group. A daemon ThreadGroup is automatically destroyed when it becomes empty. A daemon thread group can have daemon and non-daemon threads/threadGroup within itself.
I hope this helps....
Amish
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10

[ January 28, 2002: Message edited by: Marilyn deQueiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Marilyn deQueiroz:

[ January 28, 2002: Message edited by: Marilyn deQueiroz ]


Please correct me if I am wrong....
When I leave the thread to be a daemon thread, it runs for a while, then the Thread Scheduler lets the main to run, the main thread tells itself to yield and then again gives the daemon thread to run since there are no user thread left in our case the main exits, the JVM kills all the daemon threads.
When I comment the thread to be a daemon thread, the program continues forever, because the main always yield itself and let the other thread run.
Am I right, please let me know???
Thanks in advance for your help!!!
Amish
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
thanks for the code. as soon as user thread is finished the Daemon thread also dies. that part is understood.
what are Daemon groups ? what groups are we talking about here ?

Originally posted by Marilyn deQueiroz:

[ January 28, 2002: Message edited by: Marilyn deQueiroz ]
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
[QB]thanks for the code. as soon as user thread is finished the Daemon thread also dies. that part is understood.
what are Daemon groups ? what groups are we talking about here ?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi mark,
this link might be helpful...
Click Here
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setDaemon method