Hi Ying,
Ying> I want a thread to run all the time while my�Tomcat is running,
Ying> like a daemon. Can I invoke it in the init() method of my
Ying> servlet? If so, how can I check the status of the thread?
Ying> Thanks!
You should put instance of thread into ServletContext.
see following
void init( ServletConfig config){
ServletContext sc = config.getServletContext();
if ( null == sc.getAttribute("MY_THREAD")){
// create instance of thread newly
MyThread mythread = new MyThread();
// invoke thread
mythread.start();
// put thread instance to ServletContext
sc.setAttribute("MY_THREAD", mythread);
}
} //end of init
void service( ServletRequest req, ServletResponse res){
// get mythread instance set in init().
ServletContext sc = getServletContext();
Object obj = sc.getAttribute("MY_THREAD");
if(null != obj){
MyThread mythread = (MyThread)obj;
//******* check thread status here ********
System.out.println("Thread is alive : " + mythread.isAlive());
}
}
Note ServletContext exists only one for one WebApplication.
it means if another servlet (or jsp) in this webapp gets
instance of mythread using MY_THREAD as key, then it will
be same object.
Ying> Sincerely
Ying> YIng
Regards,
Watanabe.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html