Thread: Daemon Thread

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    45
    Rep Power
    0

    Daemon Thread


    What is the syntax of Daemon Thread in java?
    If it's the same as the syntax of simple thread, how does JVM know which thread is Daemon thread??

    Because as far as I know JVM treats simple Thread and Daemon Thread differently. For example if main program is going to exit and there are some threads still running JVM will wait for the threads to terminate. While if those threads are Daemon Threads JVM will simply exit the program and will terminate the Daemon Threads forcefully.
  2. #2
  3. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    13
    Thread ofcourse

    In java we have two type of Threads : Daemon Thread and User Threads. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread). User thread are generally meant to run our programm code. JVM doesn't terminates unless all the user thread terminate.
    On the other hand we have Daemon threads. Typically these threads are service provider threads. They should not be used to run your program code but some system code. These thread run paralley to your code but survive on the mercy of the JVM. When JVM finds no user threads it stops and all daemon thread terminate instantly. Thus one should never rely on daemon code to perform any program code.

    For better understanding consider a well known example of Daemon thread : Java garbage collector. Garbage collector runs as a daemon thread to recalim any unused memory. When all user threads terminates, JVM may stop and garbage collector also terminates instantly.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    45
    Rep Power
    0
    Originally Posted by codeJ
    Thread ofcourse

    Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread).
    In fact this was my Question. How do you specify a thread to be Daemon Thread? What is it's syntax??
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,756
    Rep Power
    1571
    Google and the java api are your friends. You should def learn to utilize them effectively.

    http://java.sun.com/javase/6/docs/ap...aemon(boolean)
    Open for extension, closed for modification
  8. #5
  9. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    13
    Originally Posted by adi.shoukat
    In fact this was my Question. How do you specify a thread to be Daemon Thread? What is it's syntax??
    Click the link on my previous post.
    you should be familiar with multithreading in java
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    45
    Rep Power
    0
    Originally Posted by codeJ
    Click the link on my previous post.
    you should be familiar with multithreading in java
    ahan ... now that's a perfect answer
    I'm really happy

    Thanks a lot codeJ
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    45
    Rep Power
    0
    Thanks tfecw
    Really helpful

    -- Thread Terminated --

IMN logo majestic logo threadwatch logo seochat tools logo