Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Kill a thread after a time-out

Reply
Thread Tools

Kill a thread after a time-out

 
 
Mullin
Guest
Posts: n/a
 
      04-06-2005
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
...
...

}
 
Reply With Quote
 
 
 
 
Niels Dybdahl
Guest
Posts: n/a
 
      04-06-2005
> 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


 
Reply With Quote
 
 
 
 
iksrazal
Guest
Posts: n/a
 
      04-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Mullin) wrote in message news:<(E-Mail Removed). com>...
> 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/groups?h...gle.com&rnum=6

hth,
iksrazal
http://www.braziloutsource.com/
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      04-06-2005
Mullin wrote:
> 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

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread#raise, Thread#kill, and timeout.rb are unsafe Charles Oliver Nutter Ruby 43 03-25-2008 02:31 PM
Delegate Endinvoke Kill thread chintan jajal via .NET 247 ASP .Net 1 06-06-2005 06:31 PM
KILL BABY KILL widescreen drsd2kill DVD Video 3 11-29-2004 09:36 PM
Bava's KILL BABY KILL widescreen drsd2kill DVD Video 0 11-27-2004 12:04 AM
thread, threading; how to kill a thread? Jerry Sievers Python 12 11-19-2004 12:10 AM



Advertisments