jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to generate a deamon thread in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to generate a deamon thread in java" Watch "how to generate a deamon thread in java" New topic
Author

how to generate a deamon thread in java

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
how to generate a deamon thread in java?
Thanks
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
David
From the Java API:

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.


Dave
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Also any thread spawned by a daemon thread is as well a daemon one.


SCJP2. Please Indent your code using UBB Code
sun ram
Ranch Hand

Joined: Dec 18, 2001
Posts: 61
Originally posted by Jose Botella:
Also any thread spawned by a daemon thread is as well a daemon one.

yes, child thread will have parent thread properity.
-SR


SCSecA,SCNA,SCSA,SCWCD,SCJP
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
question:
daemon threads remain or keep executing even after the JVM exits ? would that be correct to say.
If yes, then they would be like the various services running or the processes running on a NT or unix box ? would that be ok to say.
And why would one define or set a thread to be daemon ?
so threads can be daemom or a user thread (ie not a daemon). would that be ok to say.

Originally posted by Dave Vick:
David
From the Java API:
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782

daemon threads remain or keep executing even after the JVM exits ? would that be correct to say.

NO!
Daemon threads keep executing until the JVM exits. These threads are managed by the JVM and hence once the JVM exits they too are gone. Remember that JVM exits when the last user thread completes execution. This only makes sense since daemon threads are typically service threads that some long-haul background processing and their existence has no relevance when there is nobody to request ther services!
If yes, then they would be like the various services running or the processes running on a NT or unix box ? would that be ok to say.
Daemon threads are like background processes. The only difference here is that they are managed by the JVM( actually by JVM's thread scheduler ). This means the thread process(es) are owned by the JVM instance and not by the operating system. That's why they all get terminated when JVM exits.
And why would one define or set a thread to be daemon ? so threads can be daemom or a user thread (ie not a daemon). would that be ok to say
Yes, your terminology is correct. Threads can be of either user type or daemon type
Daemon threads are used ( and to be used! ) for background processing. A good example would be a spell check algorithm for a word processing software. The user interface components will be owned by the user thread however the actual spell check may be done by a daemon. As another example, you can have a daemon monitoring database connections and re-establishing them incase of a failure. As the amount of work, time and complexity increases, such processes qualify for good candidates for daemon threads. It is not unusual to see shared processes being run as daemons and accessed by many user threads.
Hope this explanation helps.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Raji Addepalli
Ranch Hand

Joined: Dec 05, 2001
Posts: 50
hi
Some threads should keep running as long as the the program is running such threads are called deamon threads.
Ex:Garbage collector.
A Java program will terminate when all the Non-deamon threads are dead.A thread is said ti die,when it comes out of the run method.A thread may dies,but still the thread Object can exist.Understand the difference keeping in view the definition we had earlier.It is not possible to creat another threadfrom the Thread Object.
Adeamon thread is automatically a deamon thread, in fact we program thread to be a deamon thread .If the code in the thread comes out of run()method it may die,even if it is specified to be a deamon thread.
A deamon thread can be created by Calling setDeamon() method in the thread class.However this should be done before execution of the thread starts.
[ January 10, 2002: Message edited by: Raji Addepalli ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to generate a deamon thread in java
 
Similar Threads
thread
How do we make a green thread
what is daemon threads
How to invoke struts action through a simple java class
about daemon thread AND JVM