Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is Demon Thread?  RSS feed

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are Demon thread? How they are benificial than other threads?
could you please explain?
regards,
Arun
 
John Dale
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When all normal (that is, non-daemon) threads terminate, the JVM terminates. daemon threads normally used for processing that is only there to support other threads. Most of the daemon threads are managed from deep within the Java API libraries.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add a thought,
you can make any worker-type thread into a daemon thread where it doesn't make sense for that thread to continue running after you "main" application threads stop. For example, you may have a background thread for doing simple animation loops. Rather than worry about cleaning up that thread when your main logic thread ends, you could just set that animation thread to be a daemon thread before you start it via a call to setDaemon(true); that way, when your main thread ends, and there are no more non-daemon threads running , the whole VM will exit.
One thing to remember is that you can only make a thread into a daemon thread before you start it. After you call start() on a thread, it's too late to set this flag and you'll get an IllegalThreadStateException.
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies...What I could understand is that I have to write something like this to make a thread demon type:
Thread t = new Thread().
t.setDemon(true)
t.start()
and the advantage for same....Am I right?
Can you also give me some reference aricle for same on net...?
regards,
Arun
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we spell it in America a "Demon" is an evil spirit. In reference to threads we usually spell it "Daemon." So if you do any websearching be sure you spell it that way.
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to mention my spelling mistake....Sure I will take care of it more in future...
But you know things will become "Demon" when they don't work

regards,
Arun
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!