Daemon Thread and Non-Daemon Thread.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Daemon Thread and Non-Daemon Thread.

    Code:
    class MainClass{
     public static void main(String a[]){
      //do something
     }
    }
    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.

    Code:
    class HelloThread extends Thread  
     {  
       public void run()  
       {  try{
         for ( ; ; )  
         {  
           System.out.println("hello");  
           sleep(1000);  
         }  }catch(Exception e){}
       }  
     }  
     public class ThreadTest  
     {  
       public static void main(String[] args)  throws Exception
       {  
         Thread hello = new HelloThread();
         hello.start();
         System.out.println("Sorry, I must be leaving");  
       }  
     }
    That means here HelloThread is non-daemon. Here main program finishes but HelloThread still running. So all the threads are by default non-daemon?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Near the start of the Thread API documentation:

    Originally posted by Thread API
    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

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      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?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Which parts of the quoted text above do you not understand?

        You don't know what a program is??

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          The second code...Main thread creating a New Thread.
          Here program means .. the creating thread. In my previous example Main Thread?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            See the quoted text above. Do you see the word "program" there?

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Yeah seen.... But here program means the Main Thread which calls the main method?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dmjpro
                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?
                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:

                Originally posted by API documentation
                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

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  You are no longer making much sense. Just read the quoted text above and stick with terminology used in that text.

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    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?

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      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

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by r035198x
                        You are no longer making much sense.
                        I know; that's what my mother already said ages ago ;-)

                        kind regards,

                        Jos

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          You snuck your response between mine and dmjpro's just to make me look silly.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by r035198x
                            You snuck your response between mine and dmjpro's just to make me look silly.
                            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

                            Comment

                            Working...