Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread timeouts

Status
Not open for further replies.

developerinlondon

Programmer
Jan 11, 2003
196
GB
The scenario is this.
a servlet creates x many threads to fetch certain documents from different locations. The servlet waits a fixed number of seconds for them to finish, if they dont finish the servlet will destroy any unfinished threads and die.

what would be the best way to do this?

I was thinking of putting all the children threads under a threadgroup and after creating all threads to sleep for the designated number of seconds, destroy all threads in the threadgroup and die. it seems destroy method is depricated in the new JDK.
I am trying to do this so that I dont have any dead threads lying around in tomcat, I always seem to be getting lots of threads hanging around doing nothing from during peak times.
 
thanks for that.
OK I am using thread.interrupt instead now, but i am getting "current thread not owner" exception, how can I make the servlet the owner of its children?
 
Code:
             ThreadGroup tg = new ThreadGroup("tg_"+request.getParameter("sessionID"));

...
// here i created a lot of threads that were part of tg threadgroup


             tg.destroy(); // this returns a null exception
     
 /*    OR I TRIED THIS ALSO which returns "current thread not owner" exception :
           Thread[] children = new Thread[tg.activeCount()];
                for (int i=0;i<tg.enumerate(children);i++) {
                    logger.info("killing "+children[i]);
                    children[i] = null;
// also tried children[i].interrupt();
                }
  */
 
Code:
Thread[] children = new Thread[tg.activeCount()];
This line will create new Threads, not get you a list of the ones in the threadgroup!!

You need to use the ThreadGroup.enumerate(Thread[] list) method, and interrupt() all the threads thereby placed in 'list'.
 
Code:
                tg.enumerate(Thread[] list);
                for (int i=0;i<tg.activeCount();i++) {
                    list[i].interrupt();
                }
so would it look like the above? I get a '.class expected' then a ') expected' error...
 
Code:
        Thread[] list = null;
                tg.enumerate(list);
                for (int i=0;i<tg.activeCount();i++) {
                    list[i].interrupt();
                }
sorry got it now.
 
The Thread[] array has to pre-exist since the method copies the list of threads into it.
 
Code:
                Thread[] children = new Thread[tg.activeCount()];
                for (int i=0;i<tg.enumerate(children);i++) {
                    children[i].interrupt();
                }

it seems to work the above code, but doesnt stop the threads. The threads remain alive and continue their work.
 
ps. I tried outputting the values of 'children' and it points to the right threads, not create new ones.
 
Do you understand what interrupt() actually does? You will still need to use a 'semaphore' method like that detailed in my above link to trigger an exit of the run() method of your Runnable classes (which is when the Thread dies).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top