• 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

Daemon thread purpose?

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, any thread can be a Daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing.

Above is written at http://www.roseindia.net/java/thread/daemon-threads.shtml.

My question here is what extra functionality we achieve when we specify any thread as daemon with calling setDaemon(true/false) ? Will there be one daemon thread by default (is it madatory)?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, that's what you get when you read roseindia tutorials. I don't recommend going there.

The API documentation (for the setDaemon() method of Thread) says

The Java Virtual Machine exits when the only threads running are all daemon threads.



So that's the purpose of making a thread a daemon thread; a running user thread will prevent the JVM from exiting whereas a running daemon thread will not.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry, that's what you get when you read roseindia tutorials. I don't recommend going there.



I don't understand your reason for criticizing roseindia tutorials.

that's the purpose of making a thread a daemon thread; a running user thread will prevent the JVM from exiting whereas a running daemon thread will not



The purpose of making a thread a daemon thread is to use it for background supporting tasks. A daemon thread is a service provider for other threads running in the same process. For example, the print spooler is a daemon thread that runs in the background and loads printing jobs (created by user threads) into a buffer (a special area in memory) where a printer can access them when it is ready.

The Java Virtual Machine exits when the only threads running are all daemon threads because there are no running user threads to service.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ogeh Ikem wrote:

Sorry, that's what you get when you read roseindia tutorials. I don't recommend going there.



I don't understand your reason for criticizing roseindia tutorials.



They are often low in quality and filled with errors. Sometimes they are good tutorials, but often those have been copied from elsewhere without attribution. And they are surrounded with so much advertising they are almost unreadable.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't understand your reason for criticizing roseindia tutorials.

They are often low in quality and filled with errors.


I second that. The code is frequently buggy, sometimes flat out wrong, and promotes bad practices. Don't go there if you value the quality of your code.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that. I can't say that I've experienced these shortcomings myself but I'll keep the information in mind.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ogeh Ikem wrote:Thanks for that. I can't say that I've experienced these shortcomings myself but I'll keep the information in mind.



I think the point is that you experienced those shortcomings in this present case.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:I think the point is that you experienced those shortcomings in this present case.



No I didn't because these 'roseindia' statements are correct.

In Java, any thread can be a Daemon thread.



Daemon threads are used for background supporting tasks and are only needed while normal threads are executing.


 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, they're both incorrect. As to #1: if all threads were daemons, then the JVM would terminate right away. As to #2: whether or not a thread should be a daemon has nothing to do with what other threads are executing at the same time.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are both correct.

As to #1: if all threads were daemons, then the JVM would terminate right away.



The 'roseindia' statement didn't say that all threads are daemons. It says that any thread can be a daemon thread. The Thread class provides the void setDaemon(boolean on) method for this purpose.

As to #2: whether or not a thread should be a daemon has nothing to do with what other threads are executing at the same time.



The 'roseindia' statement says that daemon threads are only needed while normal threads are executing. The Java API supports this by saying that the JVM exits when the only threads running are all daemon threads. This is because there are no running user threads to service.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A question of the semantics and interpretation of individual words, I suppose.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic