Limiting a function's runtime

Discussion in 'Java' started by Enter The, Nov 8, 2007.

  1. Enter The

    Enter The Guest

    Hi,

    Is there a way to limit how long a function runs for, similar to the
    timeout property for URLConnection?

    For example, the function is called getCompressedPrices(). It
    occasionally fails to return anything and freezes my application.

    I'm using an external package, and would prefer not to tinker with the
    getCompressedPrices() function.

    Regards,

    Enter
    Enter The, Nov 8, 2007
    #1
    1. Advertising

  2. "Enter The" <> wrote in message
    news:...
    > Hi,
    >
    > Is there a way to limit how long a function runs for, similar to the
    > timeout property for URLConnection?
    >
    > For example, the function is called getCompressedPrices(). It
    > occasionally fails to return anything and freezes my application.
    >
    > I'm using an external package, and would prefer not to tinker with the
    > getCompressedPrices() function.


    There is very little you can do within a JVM to tame a badly behaved
    function or thread. If an external library is unsafe a very reasonable
    approach (depending on your performance needs) is to launch it in a separate
    JVM (process), like a miniature server. (By leaving the server running you
    save startup costs.) You can then make asynchronous requests of it and
    accept timeouts. You could also monitor the health of that server
    independently and restart it if it stops responding to requests. You could
    probably build something quickly out of RMI.

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Nov 8, 2007
    #2
    1. Advertising

  3. Enter The

    Are Nybakk Guest

    Enter The wrote:
    > Hi,
    >
    > Is there a way to limit how long a function runs for, similar to the
    > timeout property for URLConnection?


    Hmm, here's an idea. You could create a thread that calls the function
    and sets a status variable when it is done. Let main (or whatever mother
    thread) sleep for a certain time and check the variable. If it didn't
    finish, terminate the thread.

    >
    > For example, the function is called getCompressedPrices(). It
    > occasionally fails to return anything and freezes my application.
    >
    > I'm using an external package, and would prefer not to tinker with the
    > getCompressedPrices() function.
    >
    > Regards,
    >
    > Enter
    >
    Are Nybakk, Nov 8, 2007
    #3
  4. "Are Nybakk" <> wrote in message
    news:...
    > Enter The wrote:
    >> Hi,
    >>
    >> Is there a way to limit how long a function runs for, similar to the
    >> timeout property for URLConnection?

    >
    > Hmm, here's an idea. You could create a thread that calls the function and
    > sets a status variable when it is done. Let main (or whatever mother
    > thread) sleep for a certain time and check the variable. If it didn't
    > finish, terminate the thread.


    Which raises the very interesting question: how do you safely terminate a
    method or thread that is running code you cannot modify? Of course, if the
    method is designed to be halted just follow its directions. I don't think
    that's generally the case, however. Interrupt() typically only applies to
    code that is waiting. Stop() and suspend () are deprecated and can leave
    the system in an invalid state. They might work, but you won't know whether
    they're safe.

    Destroy () isn't implemented (according to Javadocs), which means it has the
    same effect as ignoring the thread, but doing so allows potentially
    deadlocked or CPU-bound code to consume resources or to incur further
    problems with new tasks that are created (e.g. the broken code is holding
    locks needed by additional requests.)

    Knowing more about the code itself may lead to a technique just for that
    code, like closing a socket or something, but generally there are no
    techniques for safely stopping 3rd party code. If it really has to be safe,
    put it in a separate process.

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Nov 8, 2007
    #4
  5. Enter The

    Roedy Green Guest

    On Thu, 8 Nov 2007 16:17:16 -0500, "Matt Humphrey" <>
    wrote, quoted or indirectly quoted someone who said :

    >Which raises the very interesting question: how do you safely terminate a
    >method or thread that is running code you cannot modify?


    you can't. The method to do it is deprecated. If you try to kill it
    gently, the killed thread must co-operate. If it has gone insane, it
    may be in on position to co-operate.

    see com.mindprod.common11.StoppableThread bundled in
    http://mindprod.com/products1.html#COMMON11
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 9, 2007
    #5
  6. Enter The

    Are Nybakk Guest

    Matt Humphrey wrote:
    > "Are Nybakk" <> wrote in message
    > news:...
    >> Enter The wrote:
    >>> Hi,
    >>>
    >>> Is there a way to limit how long a function runs for, similar to the
    >>> timeout property for URLConnection?

    >> Hmm, here's an idea. You could create a thread that calls the function and
    >> sets a status variable when it is done. Let main (or whatever mother
    >> thread) sleep for a certain time and check the variable. If it didn't
    >> finish, terminate the thread.

    >
    > Which raises the very interesting question: how do you safely terminate a
    > method or thread that is running code you cannot modify? Of course, if the
    > method is designed to be halted just follow its directions. I don't think
    > that's generally the case, however. Interrupt() typically only applies to
    > code that is waiting. Stop() and suspend () are deprecated and can leave
    > the system in an invalid state. They might work, but you won't know whether
    > they're safe.


    Yes I noticed the deprecations. That was new to me. I see the API links
    to a page which describes some alternative methods to halt a thread:

    http://java.sun.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

    >
    > Destroy () isn't implemented (according to Javadocs), which means it has the
    > same effect as ignoring the thread, but doing so allows potentially
    > deadlocked or CPU-bound code to consume resources or to incur further
    > problems with new tasks that are created (e.g. the broken code is holding
    > locks needed by additional requests.)
    >
    > Knowing more about the code itself may lead to a technique just for that
    > code, like closing a socket or something, but generally there are no
    > techniques for safely stopping 3rd party code. If it really has to be safe,
    > put it in a separate process.
    >
    > Cheers,
    > Matt Humphrey http://www.iviz.com/
    >
    >
    Are Nybakk, Nov 9, 2007
    #6
  7. "Are Nybakk" <> wrote in message
    news:...
    > Matt Humphrey wrote:
    >> "Are Nybakk" <> wrote in message
    >> news:...
    >>> Enter The wrote:
    >>>> Hi,
    >>>>
    >>>> Is there a way to limit how long a function runs for, similar to the
    >>>> timeout property for URLConnection?
    >>> Hmm, here's an idea. You could create a thread that calls the function
    >>> and sets a status variable when it is done. Let main (or whatever mother
    >>> thread) sleep for a certain time and check the variable. If it didn't
    >>> finish, terminate the thread.

    >>
    >> Which raises the very interesting question: how do you safely terminate a
    >> method or thread that is running code you cannot modify? Of course, if
    >> the method is designed to be halted just follow its directions. I don't
    >> think that's generally the case, however. Interrupt() typically only
    >> applies to code that is waiting. Stop() and suspend () are deprecated
    >> and can leave the system in an invalid state. They might work, but you
    >> won't know whether they're safe.

    >
    > Yes I noticed the deprecations. That was new to me. I see the API links to
    > a page which describes some alternative methods to halt a thread:
    >
    > http://java.sun.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html


    Those notes show how to design or refactor a method or thread so that it can
    be stopped, but if you already have a method that is not well-behaved and
    cannot be rewritten you're out of luck. Wrapping a non-stoppable method
    with one that is stoppable will not produce a stoppable method.

    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Nov 9, 2007
    #7
    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. Dave Taylor

    Limiting access to user files

    Dave Taylor, Apr 2, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    375
    Kevin Spencer
    Apr 2, 2004
  2. Neven Klofutar

    Limiting Multiline textbox

    Neven Klofutar, Nov 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    5,280
    Page Brooks
    Nov 22, 2004
  3. =?Utf-8?B?UGF1bA==?=
    Replies:
    1
    Views:
    470
    =?Utf-8?B?UGF1bA==?=
    Feb 1, 2005
  4. Johnson

    Limiting Uploaded File Size

    Johnson, Feb 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    506
    Lau Lei Cheong
    Feb 19, 2005
  5. A
    Replies:
    14
    Views:
    522
    Arne Mertz
    Oct 26, 2011
Loading...

Share This Page