Originally posted by Manohar Karamballi:
Is there any way to keep thread running even after program that created thread exits? In precise , i want to start a thread in a service method of servlet and it should run even after session expires!
from the javadocs for java.lang.Thread (quote following), it would seem that you need to make sure the thread does not return from the run() method until it has finished its job - it should stay alive until then...
The Java Virtual Machine continues to execute threads until either of the following occurs:
- The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
- All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
using setDaemon(true) probably would not change anything in your case (this is based on assumption that JVM is kept running by web server threads between hits/sessions), but could actually have a detrimental effect: a daemon thread is one that would not keep a JVM running - if you set your thread to be a daemon and there were no other non-daemon threads running, your JVM would exit. this is also explained in the Thread javadocs.
hope that helps,
cheers, dan.