aspose file tools
The moose likes Threads and Synchronization and the fly likes setDaemon() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "setDaemon() " Watch "setDaemon() " New topic
Author

setDaemon()

mu gaandimara
Greenhorn

Joined: Aug 04, 2009
Posts: 10
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

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
mu gaandimara
Greenhorn

Joined: Aug 04, 2009
Posts: 10
Thank you

It means it will execute unless and until the application is stopped.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Aug 04, 2009
Posts: 10
Is it possible to know programmetically if such thread is alive or not ???
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Thread.isAlive()
mu gaandimara
Greenhorn

Joined: Aug 04, 2009
Posts: 10
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

Joined: Feb 23, 2009
Posts: 1183
You should read the Thread specification.

This should help
http://java.sun.com/docs/books/tutorial/essential/concurrency/
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
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


Experience and talent are independent of age
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
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

Joined: Aug 04, 2009
Posts: 10
I will be grateful somebody gives a program regarding this ???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35257
    
    7
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.


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: setDaemon()
 
Similar Threads
IllegalThreadStateException
Daemon Thread
setDaemon
threads!
Tomcat shutdown problem