469,300 Members | 2,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

Daemon Thread and Non-Daemon Thread.

dmjpro
2,476 2GB
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
13 8727
JosAH
11,448 Expert 8TB
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
2,476 2GB
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
r035198x
13,262 8TB
Which parts of the quoted text above do you not understand?

You don't know what a program is??
May 8 '09 #4
dmjpro
2,476 2GB
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
r035198x
13,262 8TB
See the quoted text above. Do you see the word "program" there?
May 8 '09 #6
dmjpro
2,476 2GB
Yeah seen.... But here program means the Main Thread which calls the main method?
May 8 '09 #7
JosAH
11,448 Expert 8TB
@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
r035198x
13,262 8TB
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
2,476 2GB
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
JosAH
11,448 Expert 8TB
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
JosAH
11,448 Expert 8TB
@r035198x
I know; that's what my mother already said ages ago ;-)

kind regards,

Jos
May 9 '09 #12
r035198x
13,262 8TB
You snuck your response between mine and dmjpro's just to make me look silly.
May 11 '09 #13
JosAH
11,448 Expert 8TB
@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.

Similar topics

3 posts views Thread by Mario | last post: by
25 posts views Thread by Yves Glodt | last post: by
32 posts views Thread by Adrian Herscu | last post: by
8 posts views Thread by Bern McCarty | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
12 posts views Thread by puzzlecracker | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.