wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Demon Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Demon Threads" Watch "Demon Threads" New topic

Demon Threads

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
1. What are they?
2. How are they different from regular threads?
3. Where can I find more information?
Taking the exam in less tha 10 days...
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
This is what I know about the Daemon threads. I hope this information helps you.
1. What are they?
A daemon thread is a background thread that is subordinate to the thread that creates it. Daemon thread dies when the thread that created it ends.A Daemon thread is created by method setDaemon(true). Note that you can only call setDaemon() for a thread before it starts. If you try to do so afterwards, the method will throw an IllegalThreadStateException exception.
2. How are they different from regular threads?
A thread that is'nt a daemon thread is called a user thread. A user thread has a life of it's own that is not dependent on the thread that creates it. It can continue execution after the thread that created it has ended. A user thread must be explicitly stopped or destroyed, or it's run method must return.
I have written a program to show this. In this program, the threads are daemon threads created by main(). So, when main() returns, threads will end too. To see the difference between daemon and user thread, follow the two cases mentioned below.
Case 1:
Run the program as it is.If you run the example a few times pressing Enter at random, you should see that the daemon threads die after the main() method retruns, because, from time to time, you will get some output from one or the other thread after the last output from main().
Now comment the line setDaemon(true) and see that the program runs indefinitely. This is because, the threads are user threads now and hence they are alive even though main() has returned. To stop them, just abort the program or you can code interrupts for them.

I hope the above explanation adequately answers your questions.
3. Where can I find more information?
I referred Beginning Java2 by Ivon Horton for this topic. I am sure there may be online help on this. Probably someone can help us with this.

[This message has been edited by Suma Narayan (edited June 13, 2000).]
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Suma:
You answered to my questions.
Your program was a big help.
Enjoy the day,

[This message has been edited by monty6 (edited June 13, 2000).]
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
Suma thanks for your answers but I tried your code, made some modifications to it, and find a wrong concept.
Take a look at the modified code (I modified it a lot for viewing what I�ll say):

import java.io.IOException;
public class TryThread extends Thread {
public static void main(String[] args) {
TryThread third = new TryThread();
MyThread myThread = new MyThread(third);
System.out.println("Press Enter When You Are Done......");
try {
System.out.println("Enter Pressed.....");
} catch(IOException e) {
System.out.println("Ending main() method");
public void run() {
try {
int i = 0;
for (; i++ < Integer.MAX_VALUE {
} catch(InterruptedException e) {
System.out.println("TryThread has been interrupted");
class MyThread extends Thread {
Thread third;
public MyThread(Thread third) {
this.third = third;
public void run() {
System.out.println("Thread myThread: ends!");

Suma said:

A daemon thread is a background thread that is subordinate to the thread that creates it. Daemon thread dies when the thread that created it ends.

Daemon threads doesn�t depend to the thread that they are subordinated to. Daemon threads mean that they end when there are no more no-daemon threads running.
Look at what the API documentation says:
The Java Virtual Machine exits when the only threads running are all daemon threads.
Look that MyThread ends but third (TryThread) stills executes regards the Thread that creates it (MyThread) finish and no matter third is set to daemon or non daemon. What could confused is that main method is a non daemon thread, if you instantiated another thread that is a non-daemon one, it will remain running but if it is daemon and main ends (the only non-daemon thread) the JVM finishes its execution.
[This message has been edited by Marcela Blei (edited July 26, 2000).]
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, so does this means that JVM may not finish even after the main thread had ended, provided that there are other non-daemon threads still running?
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
I am moving this topic to Threads and Synchronization

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Yes, it does mean that.
I agree. Here's the link: http://aspose.com/file-tools
subject: Demon Threads
Similar Threads
Thread Question: Are they all "under" creator?
Thread Help Wanted
For those of you who are certified ......
thread safe for SecureRandom.nextBytes()?
To which of the following can we apply the synchronized keyword