Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How setDaemon works

 
wrushasen dakhane
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please let me know how setDaemon(true) and setDaemon(false) works ..

Please explain along with the code.
wrushu
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this recent discussion.
 
wrushasen dakhane
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I gone through the discussion but still I am unable to get it.
I will paste the sample code below which I tried and did not see any difference in the output.



please explain with reference to above code snippet.

Wrushu
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being a daemon is about threads that are still running when the JVM is about to exit. The thread in your example is very short-lived, and will have finished by the time the JVM exits. Try the following code with setDaemon(true/false), and try to explain the difference.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf,

I have a confusion regarding this daemon,

When I have set, setDaemon(true), it runs continuous. But in case
of setDaemon(false), it run and stops then.

What is happening there! The thread that is running in the infinite
loop while setDaemon(false);



Please help!


Thanks,
cmbhatt
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is happening there! The thread that is running in the infinite
loop while setDaemon(false);

JVM will be alive till all the non-deamon threads have finished execution.
So, when you set the deamon flag to false, the thread is non-deamon and will keep running, however, if you set to true, as soon as the main completes execution, JVM will exit.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nitesh,

JVM wont shut down until all the non-daemon threads complete. We can force all
threads to exit before main exit by setDaemon(true);


Right???



Regards,
cmbhatt
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Chandra]: JVM wont shut down until all the non-daemon threads complete.

Right - unless System.exit() is called.

[Chandra]: We can force all threads to exit before main exit by setDaemon(true);

No. First, the term "main exit" is confusing here. There's a thread called the main thread, which is the one that runs the main method. This thread is just another non-daemon thread, nothing special about it other than the name and that it started before the others. And it may exit before or after the daemon threads, depending on how the timing works out. But the JVM will not terminate until all non-daemon threads have completed, or until System.exit() is called. When there are no more non-daemon threads, there may or may not be daemon thread, but either way, the JVM can exit, and any remaining daemon threads are killed when the JVM exits. I think when you said "main exit" you probably meant the JVM exit. But you can't force it to happen before or after the daemon threads teminate - except in the sense that when the JVM exits, it automatically terminates any remaining threads as it exits.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly Jim,

the main and JVM confused me!
Now everything alright!

main thread can exit even daemon threads are running.
"main is nothing but just another thread that runs the main method".
We can force all daemon threads to exit before JVM shuts down.
JVM won't shut down if there are still non daemon threads are running.


Question: Is there specific criteria for JVM shut down, when I made all the thread daemon, they made to exit before JVM shut down. When JVM shuts down, I never called System.exit.

Does in normal case (when there are no non-daemon threads running), JVM exit s with the main thread?




Thanks,
cmbhatt
[ April 27, 2007: Message edited by: Chandra Bhatt ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Chandra]: Does in normal case (when there are no non-daemon threads running), JVM exit s with the main thread?

The JVM will exit shortly after the last non-daemon thread exits. The last non-daemon thread may be the main thread, or it may be a different thread. It's possible the main thread exited earlier.

As part of the JVM shutdown, any remaining daemon threads will also be shut down. This isn't something you "can force" - it's something that will always happen when the JVM shuts down, if there are any daemon threads remaining.
 
wrushasen dakhane
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


For the above code in both the cases [true \ false ] I am getting the same result as below:
Output:

Main print
Runnable print
Runnable print
Runnable print
Runnable print
Runnable print
Runnable print
Runnable print
Runnable print
Runnable print
Runnable print

Please explain in detail why so?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton Jim!!!

for you!!!




Thanks,
cmbhatt
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wrushasen dakhane: The problem here is that your program is so short and quick to execute, that the Runnable has finished running before the JVM has had time to realize that it can exit. Try this modified version of the code:

Run this multiple times. You may see that the results are often or sometimes the same, but if you run it enough, you should see some variation. For fun you can experiment with changing the length of the sleep() to see how much sleep is necessary to create an observable difference in behavior. This will probably vary from one machine to another.
 
wrushasen dakhane
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim

I understood now . when we setDaemon(true) the program exits even if the child thread has not complete but it takes time to shutdown the Jvm. Hence I was unable to see the difference.

Anyway thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic