This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Unclear Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Unclear" Watch "Unclear" New topic
Author

Unclear

memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hi,
I do not exactly understand what the following try to say.
If anyone understand, please explain it to me cearly.

daemon threads do not keep the program from quitting; user threads keep the program from quitting.
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
And what does daemon threads do and why??
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
When you create a thread you can use setDaemon() to make the thread either a daemon thread or a user thread. When every user thread has finished its run method (and your main thread has finished main()), the program will quit, regardless of whether or not any threads you set to daemon are still running.

Hope that helps.
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Are there any advantage of daemon threads since they are still running even though all threads die ???
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Does the following code have the same effect with the daemon threads ??
Please explain your answer...

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by memati bas:
Are there any advantage of daemon threads since they are still running even though all threads die ???


A daemon thread doesn't keep running. Once all non-daemon threads die the JVM exits, nothing is still running at that point. The advantage to a daemon thread is it doesn't keep the JVM from terminating just becuase it's still alive. They're intended for things like background services happening that shouldn't keep an application open.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by memati bas:
Does the following code have the same effect with the daemon threads ??
Please explain your answer...



System.exit(0) will terminate the JVM, so yes it has the same affect on a daemon thread as anything else. Once that JVM terminates nothing can keep running. You can't have Word open when the computer is off anymore than you can have threads in a Java application continue to run when the JVM has terminated.
amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
hi memeti bas,


daemon threads are the threads which are running in background and are used to provide services to your application through out the life of application

take an example of linux system
in which init process(thread) is deamon thread it is parent of all process
it is providing services to new child process(thread)

in java (i am not sure but my opinion or conclusion is that gc is also deamon thread
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The following program will run until Thread t finishes, which it might never do. The single user thread t is sufficient to keep the program going.


The following program will end as soon as we exit main(). The daemon thread t is not sufficient to keep the program going.

Actually I wonder if it make take the the JVM a while to stop Thread t if t is blocking on IO or something. That might be an interesting experiment.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hi,
Thanks for replies but something make me confused about their purpose.
I do not understand the the meaning of the following sentence exactly.
Can you make it more clear ?
They're intended for things like background services happening that shouldn't keep an application open.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here are a couple real examples: I wrote a little web server with two background threads. One expires user sessions after nn minutes, another unlocks files locked for editing at a similar interval. These are daemons. When I shut down the web server there is no need for these to keep running.

On the other hand, the main() for the server is just about as short as the example I put above. It calls the application assembler to configure all the components, starts an HTTPServer thread for each port I'm listening to (the server can listen on any number of ports) and exits. The HTTPServer threads are not daemons. They run until my admin page tells them to stop so users can connect after the main() method exits.

The wording in the doc for daemon is wonderfully backwards, something about "The JVM exits when all threads that are not daemons have ended." I'm very sympathetic with anyone who needs help to get that.
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
Isn't it true that the main() method is in the daemon thread? which is also the main thread?


I'm a Hood Ornament
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Megs Maquito:
Isn't it true that the main() method is in the daemon thread? which is also the main thread?


The main method is called by the main thread, which is not a daemon thread.
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Originally posted by Stan James:
[QB]Here are a couple real examples: I wrote a little web server with two background threads. One expires user sessions after nn minutes, another unlocks files locked for editing at a similar interval. These are daemons. When I shut down the web server there is no need for these to keep running.
.
.
.


Thanks for this...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unclear
 
Similar Threads
Thread
Questions! Questions! Questions!
sabbatical
Daemon Thread
Daemon