• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Daemon Threads

 
Ahmed Matar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are Daemon threads? and what is the difference between them and "normal" threads?
 
Ivor Horton
Author
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A daemon thread is a thread that is subordinate to the thread that created it, so a daemon thread will die automatically when the thread that created it dies.
A non-daemon thread (sometimes called a user thread) is a thread that is independent of the thread that created it. When the creating thread dies, the non-daemon thread will continue to execute independently. This implies that you need to be careful to provide a sensible way to terminate non-daemon threads.
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ahmed Matar:
What are Daemon threads? and what is the difference between them and "normal" threads?

A "normal" thread is a user thread, when you explicitly call setDaemon(true) on a newly spwaned thread, it becomes daemon of the current thread. The JVM exists if there are no more user threads running, if you want JVM to terminate when main thread of your program terminates, you should spwan threads as daemon threads.
HTH,
- Manish
 
Vishakha Ahuja
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the garbage collection thread runs as a daemon thread !
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a quick example, when you start your application, a new user thread is created that execues your main method (this is sometimes called the main thread). As this is a user thread, your application will not terminate until this thread completes. At the same time that the main thread is spawned, a garbage collection thread, which is a daemon thread, will be started. As this is a daemon thread, application termination is not dependent upon the garbage collection thread. Rather, whenever the application terminates, so too does the garbage collection thread.
There is a lot of information on user and daemon threads in the Java Programmer's Certification Study forum.
I hope that helps,
Corey
 
Selvan tiru
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Corey McGlone:
Just as a quick example, when you start your application, a new user thread is created that execues your main method (this is sometimes called the main thread). As this is a user thread, your application will not terminate until this thread completes. At the same time that the main thread is spawned, a garbage collection thread, which is a daemon thread, will be started. As this is a daemon thread, application termination is not dependent upon the garbage collection thread. Rather, whenever the application terminates, so too does the garbage collection thread.
There is a lot of information on user and daemon threads in the Java Programmer's Certification Study forum.
I hope that helps,
Corey

It also depends whether your application is GUI based or character based. Generally In GUI applications once the GUI is created in main method, the Event handling thread takes the control. there is no main thread here.
--Selvan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic