Kill a thread after a time-out

M

Mullin

I have an applicatin that will create a new thread every time to do an
operation calling a third-party jar. Unfortunately, that 3rd party jar file
may hang and will not return any exception or error to my application.

Therefore, I want to kill that thread after a time-out period, say 5 minutes.
How can I implement this logic at my application?

My Main Application
===================
....
....
//new a thread to begin conversion
ConversionThread con = new ConversionThread(attachments);
Thread _thread = new Thread(con);
_thread.start();
....
....


ConversionThread
=================
// encapculate the conversion logic, calling a 3rd party java class
public class ConversionThread implements Runnable {
...
...
//calling 3rd party java class, but may hang
...
...

}
 
N

Niels Dybdahl

I have an applicatin that will create a new thread every time to do an
operation calling a third-party jar. Unfortunately, that 3rd party jar file
may hang and will not return any exception or error to my application.

Therefore, I want to kill that thread after a time-out period, say 5 minutes.
How can I implement this logic at my application?

Start a second thread which calls Sleep for 5 minutes. The thread that
terminates first kills the other thread.

Niels Dybdahl
 
I

iksrazal

I have an applicatin that will create a new thread every time to do an
operation calling a third-party jar. Unfortunately, that 3rd party jar file
may hang and will not return any exception or error to my application.

Therefore, I want to kill that thread after a time-out period, say 5 minutes.
How can I implement this logic at my application?

My Main Application
===================
...
...
//new a thread to begin conversion
ConversionThread con = new ConversionThread(attachments);
Thread _thread = new Thread(con);
_thread.start();
...
...


ConversionThread
=================
// encapculate the conversion logic, calling a 3rd party java class
public class ConversionThread implements Runnable {
..
..
//calling 3rd party java class, but may hang
..
..

}


You might want to try and hack this solution I created a while back.
It sounds like you don't actually have accees to the thread/runnable,
but the idea is to use java.util.Timer and thread.interrupt.

http://groups.google.com.br/[email protected]&rnum=6

hth,
iksrazal
http://www.braziloutsource.com/
 
T

Thomas Weidenfeller

Mullin said:
I have an applicatin that will create a new thread every time to do an
operation calling a third-party jar. Unfortunately, that 3rd party jar file
may hang and will not return any exception or error to my application.

Therefore, I want to kill that thread after a time-out period, say 5 minutes.
How can I implement this logic at my application?

You can't kill a thread in a save way. A thread has to cooperate in some
way to prematurely leave the run() method in an ordered way. The stop()
method is deprecated and can leave a real mess behind, the destroy()
method was never implemented.

Some methods can throw InterruptedExceptions when you call the
interrupt() method on the Thread object. However, if your Thread is not
hanging in such a method there is just a flag set. The thread's
implementation would have to check the flag regularly. If you can manage
to raise an InterruptedExceptions in the foreign thread it still has to
handle that exception in a way which exits the run() method instead of
e.g. just ignoring it.

Your best bet is to get that 3pp debugged. You have to be really lucky
if any of your hacking works.

/Thomas
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top