What are Daemon threads? and what is the difference between them and "normal" threads?
Ivor Horton
Author
Ranch Hand
Joined: Mar 22, 2002
Posts: 67
posted
0
A daemon thread is a thread that is subordinate to the thread that created it, so a daemon thread will die automatically when the thread that created it dies. A non-daemon thread (sometimes called a user thread) is a thread that is independent of the thread that created it. When the creating thread dies, the non-daemon thread will continue to execute independently. This implies that you need to be careful to provide a sensible way to terminate non-daemon threads.
Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Originally posted by Ahmed Matar: What are Daemon threads? and what is the difference between them and "normal" threads?
A "normal" thread is a user thread, when you explicitly call setDaemon(true) on a newly spwaned thread, it becomes daemon of the current thread. The JVM exists if there are no more user threads running, if you want JVM to terminate when main thread of your program terminates, you should spwan threads as daemon threads. HTH, - Manish
Vishakha Ahuja
Ranch Hand
Joined: Sep 13, 2000
Posts: 191
posted
0
I think the garbage collection thread runs as a daemon thread !
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
Just as a quick example, when you start your application, a new user thread is created that execues your main method (this is sometimes called the main thread). As this is a user thread, your application will not terminate until this thread completes. At the same time that the main thread is spawned, a garbage collection thread, which is a daemon thread, will be started. As this is a daemon thread, application termination is not dependent upon the garbage collection thread. Rather, whenever the application terminates, so too does the garbage collection thread. There is a lot of information on user and daemon threads in the Java Programmer's Certification Study forum. I hope that helps, Corey
Originally posted by Corey McGlone: Just as a quick example, when you start your application, a new user thread is created that execues your main method (this is sometimes called the main thread). As this is a user thread, your application will not terminate until this thread completes. At the same time that the main thread is spawned, a garbage collection thread, which is a daemon thread, will be started. As this is a daemon thread, application termination is not dependent upon the garbage collection thread. Rather, whenever the application terminates, so too does the garbage collection thread. There is a lot of information on user and daemon threads in the Java Programmer's Certification Study forum. I hope that helps, Corey
It also depends whether your application is GUI based or character based. Generally In GUI applications once the GUI is created in main method, the Event handling thread takes the control. there is no main thread here. --Selvan
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.