How to create a timer to terminate a running thread after certaintimeout value?

E

Edmond wong

Hi,

I have a thread invoking a method inside a stateless session bean. Is
there a way to terminate the thread if the method is somehow stuck. I
was thinking about using a timer of some sort, but I am not good enough
to find a way of doing it.

Thanks,

ed
 
C

Chris Smith

Edmond wong said:
I have a thread invoking a method inside a stateless session bean. Is
there a way to terminate the thread if the method is somehow stuck. I
was thinking about using a timer of some sort, but I am not good enough
to find a way of doing it.

To terminate a thread, you would need some cooperation from that thread.
Otherwise, it simply can't be done. I'm assuming that by "somehow
stuck", you mean that you can't rely on the thread to cooperate; so
you're out of luck.

If you can add some process isolation, then you could terminate the
entire containing process; which, frankly makes a lot more sense than
trying to terminate a thread and then continue the process as if nothing
were wrong.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
E

Edmond wong

Chris said:
To terminate a thread, you would need some cooperation from that thread.
Otherwise, it simply can't be done. I'm assuming that by "somehow
stuck", you mean that you can't rely on the thread to cooperate; so
you're out of luck.

If you can add some process isolation, then you could terminate the
entire containing process; which, frankly makes a lot more sense than
trying to terminate a thread and then continue the process as if nothing
were wrong.

Thanks for the reply, I mean the method that the thread calls (inside
the run method) is not returning.

Ie. I have a EJB method "(ResponseObject) process (RequestObject req)
throws EJBException". There may be a chance where the process method
got stuck, so it will block the thread from completing.

Thanks,

ed
 
K

Kevin McMurtrie

Edmond wong said:
Hi,

I have a thread invoking a method inside a stateless session bean. Is
there a way to terminate the thread if the method is somehow stuck. I
was thinking about using a timer of some sort, but I am not good enough
to find a way of doing it.

Thanks,

ed

You can create an alarm task of some kind to kill the thread if it
doesn't return to terminate the alarm before its time is up. There are
thread states that can't be interrupted, though. If you're worried
about the thread wasting time writing to a bad client, you should
instead use a socket timeout.
 
I

iksrazal

Edmond wong said:
Thanks for the reply, I mean the method that the thread calls (inside
the run method) is not returning.

I had a similair problem which I solved in a previous post:

http://groups.google.com.br/groups?...quisar&meta=group%3Dcomp.lang.java.programmer

The pooledExecutor stuff is now in java 1.5 - but the bottom line is
that the timer works in any class implementing Runnable. Make sure you
use Timer(true) if you want to exit on interrupt.

HTH,
iksrazal
http://www.braziloutsource.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top