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

Discussion in 'Java' started by Edmond wong, Mar 29, 2005.

  1. Edmond wong

    Edmond wong Guest

    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
     
    Edmond wong, Mar 29, 2005
    #1
    1. Advertising

  2. Edmond wong

    Chris Smith Guest

    Re: How to create a timer to terminate a running thread after certain timeout value?

    Edmond wong <> wrote:
    > 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
     
    Chris Smith, Mar 29, 2005
    #2
    1. Advertising

  3. Edmond wong

    Edmond wong Guest

    Chris Smith wrote:
    > Edmond wong <> wrote:
    >
    >>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.
    >


    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
     
    Edmond wong, Mar 29, 2005
    #3
  4. Re: How to create a timer to terminate a running thread after certain timeout value?

    In article <XU42e.4252$>,
    Edmond wong <> wrote:

    > 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.
     
    Kevin McMurtrie, Mar 29, 2005
    #4
  5. Edmond wong

    iksrazal Guest

    Re: How to create a timer to terminate a running thread after certain timeout value?

    Edmond wong <> wrote in message news:<Lf52e.4257$>...
    >
    > 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/
     
    iksrazal, Mar 29, 2005
    #5
    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. Peter
    Replies:
    0
    Views:
    533
    Peter
    Aug 28, 2004
  2. Todd

    Timer thread stops running

    Todd, Apr 6, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,007
  3. Vojta

    Thread - on terminate event

    Vojta, Feb 19, 2006, in forum: Java
    Replies:
    7
    Views:
    7,111
    Stefan Schulz
    Feb 20, 2006
  4. Adrian Casey

    Using signal.alarm to terminate a thread

    Adrian Casey, Nov 13, 2006, in forum: Python
    Replies:
    7
    Views:
    630
    Nick Craig-Wood
    Nov 15, 2006
  5. liu yang
    Replies:
    4
    Views:
    2,043
    Antoninus Twink
    Jul 28, 2008
Loading...

Share This Page