• Post Reply Bookmark Topic Watch Topic
  • New Topic

Daemon Thread  RSS feed

 
nsk nsk
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai this is sashi,

what is the main use of a daemon thread?how it will be implemented in a program can you give an example?

waiting for your reply bye.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the Javadoc: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html

setDaemon

public final void setDaemon(boolean on)

Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

This method must be called before the thread is started.

This method first calls the checkAccess method of this thread with no arguments. This may result in throwing a SecurityException (in the current thread).

Parameters:
on - if true, marks this thread as a daemon thread.
Throws:
IllegalThreadStateException - if this thread is active.
SecurityException - if the current thread cannot modify this thread.
See Also:
isDaemon(), checkAccess()

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Unpronounceable gibberish isn't acceptable. You can change your display name here. Thanks!

This is your second warning.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Virtual Machine exits when the only threads running are all daemon threads.


That's the important bit and I find it's worded for confusion in the JavaDoc.

Imagine a web server where the central thread manages the server socket that accepts inbound requests. We want the program to run as long as this thread runs and to stop when this thread stops. So it is NOT daemon.

There may be other threads that clean up resources or log statistics now and then. They ARE daemon. When the main thread ends we want the program to end; because these less important threads are daemons the JVM can just kill them.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!