• 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

Unclear

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what does daemon threads do and why??
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any advantage of daemon threads since they are still running even though all threads die ???
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the following code have the same effect with the daemon threads ??
Please explain your answer...

 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(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 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.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't it true that the main() method is in the daemon thread? which is also the main thread?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...

 
reply
    Bookmark Topic Watch Topic
  • New Topic