File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes daemon Vs User Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "daemon Vs User Thread " Watch "daemon Vs User Thread " New topic
Author

daemon Vs User Thread

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi
1) Daemon thread stop execution as main() thread dead.
2) User thread can run even main thread is dead.
Now Q. which are true . (select 1)
a) a prog ends when all deamon threads end.
b) a prog ends when all user threads end.
c) a prog ends when all threads end.
d) a user thread can not stop daemon therad.
I thought wgen all threads end then prog ends .. but here they say ....
a)a prog ends when all deamon threads end.
any one ... plz why ??
TIA


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
ravish,
in Java there are two distinct types of threads: the user threads and the deamon threads. They have exactly the same interface, however, user threads are usually created by the user and deamon threads are usually created by the JVM (or the user by invoking setDeamon(true) on them). The thing is that deamon thread only live to serve user threads. If all user threads have died, there is no reason for deamon threads to continue since nothing will be needed from them anymore.
An example of deamon threads is the garbage collection thread created by the JVM. When there is no more user threads running there is no need to garbage collect unreferenced objects, so there is no need for the garbage collection deamon thread anymore.
From this you can easily answer the questions you posted
HIH
[ January 29, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
OK, I inderstand your explanation. When all user threads dies then all daemon threads dies too.
But, A Java program ends when all daemon threads ends? Why?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Carlos R Ram�rez:
OK, I inderstand your explanation. When all user threads dies then all daemon threads dies too.
But, A Java program ends when all daemon threads ends? Why?

Where did you get this question from? Personally, I would answer it by saying that a program ends when all user threads end. When that occurs, all daemon threads automatically end.
It doesn't really make sense (at least to me) that a program terminates when all daemon threads terminate because a program might not even have a daemon thread (although it will have at least one user thread). If a program terminates when all daemon threads terminate, your program might be considered "terminated" before it begins.
I don't know where this question came from, but that's the answer that seems to make the most sense to me. Anyone else?
Corey


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Actually the given answer is wrong. A Java program ends when all user threads end.
Since we don't want to reinvent the wheel here, let's use the search engine and find some good discussion about deamon threads. OK?
Waw, here is a good one:
http://www.javaranch.com/ubb/Forum24/HTML/001830.html
and here is another one:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=27&t=000109
and another one:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=27&t=000376
Amazing what a search engine can do for you !!!
Moreover, the same topic was discussed yesterday:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014519
HIH
[ January 29, 2002: Message edited by: Valentin Crettaz ]
[ January 29, 2002: Message edited by: Valentin Crettaz ]
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
I got this from a mock test:
when JVM exists the main method, it will stop only after all the threads are stopped.
if there are multiple threads, reading or writing of data of class is inconsitent.
This is wrong since the JVM exits once only dameon threads have stoppped, correct?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
HI Val
thanks a lot for ur links ...
and as per the link http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=27&t=000376
a prog ends when all user threads end..
I think it is true also ... as when user thread dies .. deamon dies too...
but very much posiible if I do not create any daemon therad .. then ... correct ans is when all user threads die..
NO... I think he is considering GC thread also ... and considering that .. YES I think I was wrong ... correct ans should be
a) when all daemon therads end.
(edited:
http://www.javaranch.com/ubb/Forum24/HTML/001830.html
look this thread here "Marcela Blei" ... as per him Java doc says ... JVM exits when there is only daemon thrd are running ... I think when JVM eixt then a prog ends.... so ans is one. CMIW
)
CMIW
TIA
[ January 29, 2002: Message edited by: ravish kumar ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
A Java program ends when no more user threads are running, or put differently, when only deamon threads are running.
So the correct answer should be
b.a prog ends when all user threads end.

HIH
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi Val ..
NOW i am confuse .. this que is from JQ+
JQ+ says ..when all daemon...
API says ..JVM exits when there is only daemon thrd are running.
U say. when all user
in support of JQ+ .. i can say that when JVM exits means prog ends.
BUT I want to know the correct ans..
Plz Val, Marcus, Jose .... anyone ..
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
any one ... any comment PLZ
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Ravish,
When your application starts, the JVM puts it in its own thread, a user thread to be exact, and also creates a bunch of deamon threads (garbage collection thread, event dispatching thread, etc). From there on, if you don't create any new (user) thread, then the program ends when the end of the main method is reached because the only user thread that was created has jut ended. What is the purpose of deamon threads if they don't have any user thread to serve? That's the question you should ask yourself. What would the GC thread garbage-collect if no user threads is creating new objects, or what is the purpose of having an event-dispatching thread if there are no more events to deliver?
So the JVM exits when there are no more user threads running, which is equivalent to say that it exits when only deamon threads are running.
HIH
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by ravish kumar:
any one ... any comment PLZ

I still stand by my explanation. When a program is started a single user thread is created for that program. That user thread can then spawn as many (or few) user and/or daemon threads as it wants. The user threads will continue to execute until they either complete their tasks or until they are stopped. The daemon threads, on the other hand, will stop when they complete their tasks, are stopped, or when all user threads for that program are stopped. Therefore, a program ends when all user threads are stopped, as that will also terminate any and all daemon threads that are running. Let's look at the following code:

If I run this application as is, I get the following output:

Notice that line 1 states that there are no more daemon threads running, yet the program continues to execute. (Therefore, a program does not end when there are no more daemon threads running.) Also, if you notice line 2, the final thing that is written out is that there are no more user threads running. It would seem that the program ends when there are no more user threads running.
Now, let's see what happens when you comment out the line "userThread.start();" This is the output I get:

Notice that, at 1, we have started a daemon thread. The program ends after line 2 is printed, stating that there are no more user threads. Yet, the daemon thread that has started never finished! Why not? Because daemon thread are automatically stopped when all user threads are stopped.
So, based upon all this mumbo-jumbo, I claim that a program ends when all user threads are terminated (including the one that is started by invoking the program to begin with). Use this code and play with it to see what you can find out. If someone can disprove me, I'd love to hear it, but this is my story and I'm stickin' to it.
I sure hope this clears things up,
Corey
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Corey, outstanding explanation proving the concept
ravish, I think the point should be clear after this "mumbo-jumbo"
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by ravish kumar:
any one ... any comment PLZ

I still stand by my explanation. When a program is started a single user thread is created for that program. That user thread can then spawn as many (or few) user and/or daemon threads as it wants. The user threads will continue to execute until they either complete their tasks or until they are stopped. The daemon threads, on the other hand, will stop when they complete their tasks, are stopped, or when all user threads for that program are stopped. Therefore, a program ends when all user threads are stopped, as that will also terminate any and all daemon threads that are running. Let's look at the following code:

If I run this application as is, I get the following output:

Notice that line 1 states that there are no more daemon threads running, yet the program continues to execute. (Therefore, a program does not end when there are no more daemon threads running.) Also, if you notice line 2, the final thing that is written out is that there are no more user threads running. It would seem that the program ends when there are no more user threads running.
Now, let's see what happens when you comment out the line "userThread.start();" This is the output I get:

Notice that, at 1, we have started a daemon thread. The program ends after line 2 is printed, stating that there are no more user threads. Yet, the daemon thread that has started never finished! Why not? Because daemon thread are automatically stopped when all user threads are stopped.
So, based upon all this mumbo-jumbo, I claim that a program ends when all user threads are terminated (including the one that is started by invoking the program to begin with). Use this code and play with it to see what you can find out. If someone can disprove me, I'd love to hear it, but this is my story and I'm stickin' to it.
I sure hope this clears things up,
Corey
[ January 30, 2002: Message edited by: Corey McGlone ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi
I am with u all
but what abt GC thread ... that is created by JVM ... If you consider thta also then a prog ends when all daemon thread ends (they might be created by JVM..)
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by ravish kumar:
Hi
I am with u all
but what abt GC thread ... that is created by JVM ... If you consider thta also then a prog ends when all daemon thread ends (they might be created by JVM..)

It is true that, if all user threads complete and there are daemon threads executing, those daemon threads then terminate. Therefore, in the case of the garbage collection thread that you've brought up, the program ends when all daemon threads complete. BUT that's not why the program terminated. The program really terminated because all user threads ended. It just so happened, in this case, that all daemon threads ended at the same time.
Look back at the code I showed. You can see in the first example that all of the daemon threads had terminated but the program continued to execute. Due to that, it should be clear that the termination of all daemon threads does not cause a program to terminate. Only the cessation of all user threads will cause that.
Just because the termination of all daemon threads happens to coincide with the termination of all user threads (and hence the program) doesn't mean that the program ended because all of the daemon threads ended. That's faulty cause and effect. It's equivalent to saying that because I stepped outside, it started raining. The fact that the two things happened at the same time is more of a conincidence than a cause and effect relationship. Obviously, there is somewhat of a relationship as to why the program terminated along with all of the daemon threads (ahem, because all of the user threads ended), but saying that one caused the other isn't right.
I hope I haven't managed to confuse you even more.
Corey
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
as per me too
either ans should be All thread OR all user thread as when user thread ends .. deamon thread ends too....(main is also UserThread.. and all created thread are also UserThrad ).. but JQ+ says something else ..
let us see .. what comes in exam ... Now I will tick User ends ... after reading que carefully
Thhanks a lot ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: daemon Vs User Thread
 
Similar Threads
Threads
Another JQ+ Thread
Thread Problem
daemon thread and heavyweight concept
what are daemon threads?