Kill a thread after a time-out

Discussion in 'Java' started by Mullin, Apr 6, 2005.

  1. Mullin

    Mullin Guest

    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
    ...
    ...

    }
     
    Mullin, Apr 6, 2005
    #1
    1. Advertising

  2. > 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
     
    Niels Dybdahl, Apr 6, 2005
    #2
    1. Advertising

  3. Mullin

    iksrazal Guest

    (Mullin) wrote in message news:<>...
    > 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?&rnum=6

    hth,
    iksrazal
    http://www.braziloutsource.com/
     
    iksrazal, Apr 6, 2005
    #3
  4. 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/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, Apr 6, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chintan jajal via .NET 247

    Delegate Endinvoke Kill thread

    chintan jajal via .NET 247, May 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,989
    Richard Grimes [MVP]
    Jun 6, 2005
  2. Rasmus B. Nielsen

    thread kill ?

    Rasmus B. Nielsen, Aug 30, 2003, in forum: Java
    Replies:
    0
    Views:
    493
    Rasmus B. Nielsen
    Aug 30, 2003
  3. Jerry Sievers

    thread, threading; how to kill a thread?

    Jerry Sievers, Nov 17, 2004, in forum: Python
    Replies:
    12
    Views:
    1,184
    Mustafa Demirhan
    Nov 19, 2004
  4. flamesrock
    Replies:
    8
    Views:
    548
    Hendrik van Rooyen
    Nov 24, 2006
  5. Charles Oliver Nutter
    Replies:
    43
    Views:
    664
    Roger Pack
    Mar 25, 2008
Loading...

Share This Page