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.
Joined: Mar 22, 2005
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.
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]: 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
Joined: Feb 28, 2007
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?
[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.
Joined: Sep 25, 2006
For the above code in both the cases [true \ false ] I am getting the same result as below: Output:
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.
Joined: Sep 25, 2006
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.