aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thred Should run even after Program terminates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thred Should run even after Program terminates" Watch "Thred Should run even after Program terminates" New topic
Author

Thred Should run even after Program terminates

Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai All!
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!
ASAP! anuy help is appreciable!
from
Manohar
boba fett
Greenhorn

Joined: Jul 19, 2001
Posts: 4
Your thread problem appears to be how to keep a thread running after a program exits? Well your solution is actually hidden in what main is. Main is a thread in the process. It is a DEAMON. Well once main exists, your program exits even if you have threads running right. Well the solution is there is a mehtod call in the thread class, look into the API. It is something like SetThreadToDEAMON(). Well once you set a thread to deamon, then the actually program will not exit, hence the program will not die till the thread eventually dies. But in the background the execution of your normal main() procedural program will have finished, but the program will not die till the thread does. This is the only way. Threads in JAVA are threads in the VM. Threads in Windows or so on are actually independent processes, that have their own space in the kernel processes space. So simple assign that thread to a DEAMON and the thread will run even after the main() has finished executing.
boba fett
Greenhorn

Joined: Jul 19, 2001
Posts: 4
This is the method call on your thread. I hate it when people sometimes give me all but the actual syntax. So here it is

void setDaemon(boolean on)
Marks this thread as either a daemon thread or a user thread.
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

hi boba,
As far as i know main thread is a user/worker thread not a daemon thread. Correct me if i m wrong!

------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
I think Ashik is right.
[This message has been edited by Snylt Master (edited July 26, 2001).]


Preparing for the Java 2 Certification exam
Dmitry Shekhter
Greenhorn

Joined: Feb 21, 2001
Posts: 26
I don't know exactly what your thread does, but if it's some sort of a utility thread that you want to run even after service() returns, consider creating it in the init() method of the servlet.
knex knex
Greenhorn

Joined: Jun 15, 2001
Posts: 2
try this:
public static void main(String[] args) {
Thread t = new Thread();
System.out.println("isDaemon? "+t.isDaemon());
}
system:> isDaemon? false
the child thread always get the thread-type form its father.
so main is a user-thread.
dan moore
Ranch Hand

Joined: Nov 04, 2001
Posts: 68
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.


dan moore, infomatiq ltd.<br />email dan@infomatiq.co.uk<br /><a href="http://www.infomatiq.co.uk" target="_blank" rel="nofollow">http://www.infomatiq.co.uk</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thred Should run even after Program terminates