This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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:
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).
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 ]