Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Daemon Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Daemon Thread" Watch "Daemon Thread" New topic
Author

Daemon Thread

rekha devan
Greenhorn

Joined: Oct 10, 2005
Posts: 26
Hi,

How does daemon thread work? I have a code wherein I set the the thread as daemon before starting it . But what I observe is that even after the main dies the daemon thread continues executing.

thanks,
Rekha
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

When all user (non-daemon) threads dies, the JVM will exit.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
rekha devan
Greenhorn

Joined: Oct 10, 2005
Posts: 26
I guess the it does not happen immediately.

Like in this case I created one new thread and made it daemon thread by setDaemon(true). After initializing the Daemon thread the main exits, the daemon thread continues to execute.

Now I tried putting sleep(500) in the run method.Then it exits.

so what i conclude from this is that even if the main thread dies, the daemon thread remains active and continues to execute for a certain period of time only then dies. Now it depends if your code is small it might get executed completed else it might execute only partially. But there is not guarantee.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please post your code where the behaviour you observed is obtained.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
rekha devan
Greenhorn

Joined: Oct 10, 2005
Posts: 26
Hi,

below is the code where inconsistency was observed.

[ October 15, 2005: Message edited by: Barry Gaunt ]
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

I Got theis excerpt for Sun\s Java API :
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

* The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
* All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thanks for posting the code. I ran it without the sleep. Yes, it does look as if the started thread does get some time to run, but it does not always run to completion - it terminates during the loop when it gets whatever signal is sent to it when the JVM detects that the main thread dies. The behaviour is as expected.
 
 
subject: Daemon Thread
 
Similar Threads
Whats is Late Binding
Java Thread
Daemon Threads
Daemon threads & non daemon Threads
Difference between start() and run() method?