File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Daemon thread purpose?

 
scott miles
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • 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)?
 
Paul Clapham
Sheriff
Pie
Posts: 20153
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 20153
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question of the semantics and interpretation of individual words, I suppose.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic