File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Daemon thread purpose? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Daemon thread purpose?" Watch "Daemon thread purpose?" New topic
Author

Daemon thread purpose?

scott miles
Ranch Hand

Joined: Jun 16, 2011
Posts: 70
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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: May 13, 2002
Posts: 180
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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
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

Joined: May 13, 2002
Posts: 180
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
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

Joined: May 13, 2002
Posts: 180
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
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
A question of the semantics and interpretation of individual words, I suppose.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Daemon thread purpose?