• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Daemon Thread

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is a daemon thread? How does it differ from a non-daemon thread...
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akhil Trivedi:
What is a daemon thread? How does it differ from a non-daemon thread...



Well, whenever a standalone application is started, a user thread is automatically created which executes the main() method.

All other threads are spawned from the main() mehtod. After spawning child threads, the main() method can finish but the program will keep running unless all child threads complete.

However, if a thread is declared as daemon, it ensures that the thread will be terminated as soon as the main() method terminates.

Basically daemon threads are there only 2 serve user threads.

Hope this helps !!

Cheers !!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JavaDoc is confusing doublespeak, something about "the program can end when all threads that are not daemon have ended." It doesn't explicitly say that it doesn't care about threads that are daemon but the program can end whether there are daemon threads or not.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akhil Trivedi:
What is a daemon thread? How does it differ from a non-daemon thread...




I don't completely hate the name, because it harks back to the early days of UNIX, when the terminology was ... um ... colorful. There's a thought-experiment in physics called "Maxwell's daemon", where Maxwell imagined an invisible daemon sitting at the mouth of a cave, letting only hot molecules in and cold molecules out. So the cave gets hotter and hotter. Maxwell was educated in the 19th century, hence the odd spelling.

So a daemon is a mysterious and invisible thing. A Java application has two kinds of thread. First there's the visible and evident kind, known as user threads. When your application starts up, the JVM creats one such thread and tells it to call your main() method. Any other threads spawned off by this thread are also user threads. Meanwhile, sneaking around behind the scenes on your behalf, working mysteriously and invisibly, are daemon threads. The big ones are the garbage collector and the event-andling threads. You don't create them, you don't control them, you don't have to think about them very much. And they don't ever die, because your program can't survive without them.

The JVM decides it's time to be done when all of your user threads are done. If daemon threads are running, that's ok: that's just infrastructure.


-- Phil
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daemon threads are not exclusively the domain of JVM infrastructure, though that may be the most common use. We can make our own (set deamon true on a thread before we start it) if we don't care about shutting it down at the end of the program. Say I have a thread that periodically checks a directory to see if there are any new files. If I make that a daemon I can shut down the main user thread and the deamon just goes away. If I don't make it a daemon I have to be sure to terminate it before exiting the program.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
User threads make your program keep running as long as one of them is running. Daemon threads do not keep your program from exiting. Thus daemon threads can be rudely interrupted by an exiting program.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic