• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Daemon Threads

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are Daemon threads? and what is the difference between them and "normal" threads?
 
Author
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the garbage collection thread runs as a daemon thread !
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic