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
posted
0
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
posted
0
Thank you
It means it will execute unless and until the application is stopped.
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
posted
0
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
posted
0
Is it possible to know programmetically if such thread is alive or not ???
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
posted
0
Thread.isAlive()
mu gaandimara
Greenhorn
Joined: Aug 04, 2009
Posts: 10
posted
0
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 ???
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
posted
0
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
posted
0
I will be grateful somebody gives a program regarding this ???
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...
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.