Advertisements

> Is there a way to call a method with a timer? Such that if the method
> does not complete before the timer hits 0, the method returns false.
>
> for example:
>
> int i=0;
> while ( !methodInvaction() && i++<3 ) {
> }
> boolean methodInvocation() {
> //
> ...
> //
> return true; (2)
> }
>
> A timer would be added such that if methodInvocation doesn't hit (2)
> in, say, x milliseconds, the method would return false. And 'i' is
> just to have it try upto 3 times.

You might implement it with two threads, one for executing the function and
one for doing the timing. The one that finishes first should then stop the
other.

Advertisements

Niels Dybdahl wrote:
>>A timer would be added such that if methodInvocation doesn't hit (2)
>>in, say, x milliseconds, the method would return false. And 'i' is
>>just to have it try upto 3 times.
>
>
> You might implement it with two threads, one for executing the function and
> one for doing the timing. The one that finishes first should then stop the
> other.

Except that threads cannot be stopped. The stop() method was depreceated and
is nonfunctional because it could leave the application in an inconsistent
state.

Basically, the above is possible only inasmuch as you can let the method run
on regardless until it (and the thread) terminates naturally. Of course this
could leave your system swamped with abandoned threads hogging all the CPU
cycles. This "method timeout" is a nasty hack anyway - if you want your
stuff to terminate, program it so that it does!

In article <c639t8$7h8ge$-berlin.de>,
Michael Borgwardt <> wrote:
>
>Except that threads cannot be stopped. The stop() method was depreceated and
>is nonfunctional because it could leave the application in an inconsistent
>state.

While there is no safe _general_ way of stopping threads, you can
happily code your own thread in such a way that _it_ can be safely
stopped. Not by using Thread.stop() though mind you

Michael Borgwardt wrote:
> Niels Dybdahl wrote:
>
> Except that threads cannot be stopped. The stop() method was depreceated
> and
> is nonfunctional because it could leave the application in an inconsistent
> state.

Of course they can. You just can't call Thread.stop() to stop them.
Threads stop when their run() terminates. The key is to cause the run()
to terminate when you want it to.
> Basically, the above is possible only inasmuch as you can let the method
> run
> on regardless until it (and the thread) terminates naturally. Of course
> this
> could leave your system swamped with abandoned threads hogging all the CPU
> cycles. This "method timeout" is a nasty hack anyway - if you want your
> stuff to terminate, program it so that it does!

This question comes up all the time and we always have the "can't call
stop() on a thread discussion." Write your code so that when the timer
is through it will cause your run() to end. Thread has three methods to
make this easy, interrupt(), interrupted(), and isInterrupted(). If you
are using a Thread.sleep() in your run() you can just exit the method in
your exception handler. If you are blocked on I/O you can close the
stream in the timer and exit the run() the same way.

On 20 Apr 2004 05:59:55 -0700, (Marwan) wrote or
quoted :
>Is there a way to call a method with a timer? Such that if the method
>does not complete before the timer hits 0, the method returns false.

that's called a timeout. You will find that sort of code for example
in a socket read.

Basically, you set a timer, if the work completes, it turns off the
timer. If the timer finishes first, it cancels the work, and returns
an error code.

See http://mindprod.com/products.html#BUS which includes
StoppableThread which is a polite way to stop the task that did not
finish in time. This won't work for a thread that has gone nuts, and
is hanging in a tight loop.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!