• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

daemon Vs User Thread

 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any one ... any comment PLZ
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey, outstanding explanation proving the concept
ravish, I think the point should be clear after this "mumbo-jumbo"
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic