By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,469 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,469 IT Pros & Developers. It's quick & easy.

Daemon Thread and Non-Daemon Thread.

dmjpro
100+
P: 2,476
Expand|Select|Wrap|Line Numbers
  1. class MainClass{
  2.  public static void main(String a[]){
  3.   //do something
  4.  }
  5. }
  6.  
When i run this class file then JVM is the process having one main thread.
Thread ends and process ends.
I went through a link there i read about daemon thread.
"a daemon thread is one which won't prevent the program from ending." Basically it means main program/main thread.

Expand|Select|Wrap|Line Numbers
  1. class HelloThread extends Thread  
  2.  {  
  3.    public void run()  
  4.    {  try{
  5.      for ( ; ; )  
  6.      {  
  7.        System.out.println("hello");  
  8.        sleep(1000);  
  9.      }  }catch(Exception e){}
  10.    }  
  11.  }  
  12.  public class ThreadTest  
  13.  {  
  14.    public static void main(String[] args)  throws Exception
  15.    {  
  16.      Thread hello = new HelloThread();
  17.      hello.start();
  18.      System.out.println("Sorry, I must be leaving");  
  19.    }  
  20.  }
  21.  
That means here HelloThread is non-daemon. Here main program finishes but HelloThread still running. So all the threads are by default non-daemon?
May 7 '09 #1
Share this Question
Share on Google+
13 Replies


Expert 10K+
P: 11,448
Near the start of the Thread API documentation:

Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class).
kind regards,

Jos
May 7 '09 #2

dmjpro
100+
P: 2,476
Well ....
I have some definitions of threads. Daemon means Devil(violates the normal actions). Is it?
Daemon Thread: It won't prevent program from ending. That means if the wants to stop and then Daemon Thread won't stop it from ending.
Anything wrong in my conception here?
Here the term program, what does it mean? Please clarify me!

In my case the New Thread is Daemon or Non-Daemon and Why?
May 8 '09 #3

10K+
P: 13,264
Which parts of the quoted text above do you not understand?

You don't know what a program is??
May 8 '09 #4

dmjpro
100+
P: 2,476
The second code...Main thread creating a New Thread.
Here program means .. the creating thread. In my previous example Main Thread?
May 8 '09 #5

10K+
P: 13,264
See the quoted text above. Do you see the word "program" there?
May 8 '09 #6

dmjpro
100+
P: 2,476
Yeah seen.... But here program means the Main Thread which calls the main method?
May 8 '09 #7

Expert 10K+
P: 11,448
@dmjpro
I had hoped that the little quote had tickled your curiosity; if you had read the API documentation, you'd have seen the next sentence:

The Java Virtual Machine continues to execute threads until either of the following occurs:

* The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.

* All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
The main thread is not a daemon thread and when it dies and no other non-daemon threads are alive the entire jvm will shut down.

kind regards,

Jos
May 8 '09 #8

10K+
P: 13,264
You are no longer making much sense. Just read the quoted text above and stick with terminology used in that text.
May 8 '09 #9

dmjpro
100+
P: 2,476
But the thing is in my example the New Thread is Daemon or Non-Daemon?
As Josh said if no Non-Daemon threads alive then JVM shuts down. If the creating thread is Daemon then new threads will be Daemon. Is it applicable for Non-Daemon? i think .. no ;)
Another tutorial says .... Daemon thread is the service providers for other threads.
Here other threads mean both or Non-Daemon only? So "if no Non-Daemon threads alive then JVM shuts down" this line valid. If there no Non-Daemon threads then to whom service to be provided.
Up to this is cleared so far (having some confusion what i asked).
Now come to my program... The main thread creates the new thread which is simply running an infinite loop. Now here main thread ends but the new thread still running. If the set the Daemon flag true using setDaemon(true) then new thread end up before the main thread. So the new thread is a non-daemon as it changes the behaviour after changing the daemon flag to true. In which sense the new thread is called Non-Daemon?
May 8 '09 #10

Expert 10K+
P: 11,448
There is nothing special about deamon threads or non-daemon threads. A (non)daemon thread creates (non)daemon threads. The status of a thread can be changed with the setDaemon(...) call before a thread is started.

If no non-daemon threads are alive anymore the JVM will quit. That is all there is to it.

kind regards,

Jos
May 8 '09 #11

Expert 10K+
P: 11,448
@r035198x
I know; that's what my mother already said ages ago ;-)

kind regards,

Jos
May 9 '09 #12

10K+
P: 13,264
You snuck your response between mine and dmjpro's just to make me look silly.
May 11 '09 #13

Expert 10K+
P: 11,448
@r035198x
I didn't do it! I'm innocent! One of my parrots walked over my keyboard; naughty parrot! No cookie! Naughty!

kind regards,

Jos :-D
May 11 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.