aspose file tools*
The moose likes Threads and Synchronization and the fly likes How setDaemon works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How setDaemon works" Watch "How setDaemon works" New topic
Author

How setDaemon works

wrushasen dakhane
Ranch Hand

Joined: Sep 25, 2006
Posts: 47
Hi,

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

Please explain along with the code.
wrushu


SCJP 1.4, SCWCD 1.4
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41529
    
  53
Take a look at this recent discussion.


Ping & DNS - my free Android networking tools app
wrushasen dakhane
Ranch Hand

Joined: Sep 25, 2006
Posts: 47
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
Marshal

Joined: Mar 22, 2005
Posts: 41529
    
  53
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

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638


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.


apigee, a better way to API!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Jan 30, 2000
Posts: 18671
[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

Joined: Sep 25, 2006
Posts: 47


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

Joined: Feb 28, 2007
Posts: 1707
Thanks a ton Jim!!!

for you!!!




Thanks,
cmbhatt
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Sep 25, 2006
Posts: 47
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.
 
Don't get me started about those stupid light bulbs.
 
subject: How setDaemon works