Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Daemon Thread

 
rekha devan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When all user (non-daemon) threads dies, the JVM will exit.

Henry
 
rekha devan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code where the behaviour you observed is obtained.
 
rekha devan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

below is the code where inconsistency was observed.

[ October 15, 2005: Message edited by: Barry Gaunt ]
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic