• Post Reply Bookmark Topic Watch Topic
  • New Topic

setDaemon()

 
mu gaandimara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a llittle RnD on setDaemon() method of Thread and found that it does nothing.

What is the use of this method ???
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say your java application employs 3 Threads. None of them is a deamon.

This means, the application will not shut down until all Threads have finished their work.

Now, if Thread A is non-deamon, Thread B and C are deamons, and Thread A finishes work, Thread B and C are just killed and the application shuts down.

This is helpful for background tasks.
 
mu gaandimara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you

It means it will execute unless and until the application is stopped.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it means the application doesn't care to wait for the Deamon Threads to finish. As soon as the regular threads are done, the deamons are killed in the middle of what they were doing.

Try this



run this, and then remove the setDeamon and run it again.
 
mu gaandimara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to know programmetically if such thread is alive or not ???
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.isAlive()
 
mu gaandimara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I know this ???

The daemon thread will not die as long as any other threads exist. If all threads die, how can we call isAlive() ???

Can we make the thread object call any method when the thread dies ???
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should read the Thread specification.

This should help
http://java.sun.com/docs/books/tutorial/essential/concurrency/
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.isAlive() should be used carefully in such a scenario. It would be fruitful if it is used before the completion of the normal thread. If the non-daemon has already ended, Thread.isAlive is bound to return false.

Regards
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rebius hagrid wrote: Can we make the thread object call any method when the thread dies ???

Anything that happens in your thread, or in other words, anything your thread does is there written by you in the run() method or however you pragrammed it. Before run() ends, you can do a notificatoin.

But there are other better ways to make notifications, the link marked by Sebastian is really helpful
 
mu gaandimara
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will be grateful somebody gives a program regarding this ???
 
Henry Wong
author
Sheriff
Posts: 22537
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I know that I am going to catch flak for this, but... most of the time, this functionality won't be used. It is rare that an application will just have the user threads terminate, and use the daemon criteria to determine whether a JVM will exit.

Most of the time, the exit criteria is determine by a very specific event, which triggers a shutdown (saving resources, etc) and exit of the JVM (using System exit).

Interesting feature. Nice to know. But in most cases, not used...

Henry
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find daemon threads useful for background tasks that don't have any special cleanup needs. In other words, nobody gets bothered if the thread just goes away in the middle of doing something because the JVM quits, or the web app is stopped, or whatever. If those threads are daemons, they don't need to be stopped explicitly - so there's no need to keep track of them, or to add logic to terminate them gracefully.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!