• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What exactly are daemon threads?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having trouble understand the concept of daemon threads. Can a user explicitly start these? Can someone explain in detail?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM categorizes Threads into two groups :
user and daemon.
when initially created threads are user threads,but before they are started they may be tagged as daemon threads by the setDaemon method.This tag becomes important when the JVM tries to decide when to shut down a program .
Every time a thread dies,The JVM looks at the remaining threads to see whether any of them is a user thread.If only daemon threads are left,the JVM terminates.
Hope Iam clear.
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daemon thread are basically used for services. They serve other thread's requirements. If a thread group is a daemon thread group then all the threads created with that thread group default to daemon threads. SetDaemon() method is also available on thread group to make any thread group a daemon thread group.
Any exception that is raised in daemon threads are ignored by JVM.
Please correct me if any thing I mentioed here is wrong. My knowledge is based on reverse enginnering from Mocks.
 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, If understand it correctly , a daemon threda has to be explicityly set by the developer. On it's own none of threads sstarted by the system are daemon threads?
Am I correct in assuming that?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, If understand it correctly , a daemon threda has to be explicityly set by the developer. On it's own none of threads sstarted by the system are daemon threads?
If one daemon thread creates another thread then that will also be a daemon thread.
watch for the following from Thread API


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.


------------------
Every time a thread dies,The JVM looks at the remaining threads to see whether any of them is a user thread.If only daemon threads are left,the JVM terminates.
You are right. So there is no guarantee that the Daemon thread will get a chance to run. The life of Daemon depends on the user thread of the program.
Ambapali
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, If understand it correctly , a daemon threda has to be explicityly set by the developer. On it's own none of threads sstarted by the system are daemon threads?
No, the system can start threads on its own for various reasons, which may be daemon threads. The main example I know of is garbage collection - it's performed by a low-priority daemon thread started by the system without any explicit command. (Unless GC is caused by System.gc() or an impending OutOfMemoryError, in which case it's probably being done by the same thread that called gc() or attempted to allocate new memory.)
Any thread started by the programmer will be a non-daemon thread, unless (a) it's explicitly set to be a daemon thread, or (b) it's created from within another daemon thread.
 
reply
    Bookmark Topic Watch Topic
  • New Topic