Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thred Should run even after Program terminates

 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Snylt Master
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Ashik is right.
[This message has been edited by Snylt Master (edited July 26, 2001).]
 
Dmitry Shekhter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic