Method Timout??

M

Marwan

Hi all,

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.


Thanks,
Marwan
 
N

Niels Dybdahl

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.

Niels Dybdahl
 
M

Michael Borgwardt

Niels said:
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!
 
C

Chris Smith

Marwan said:
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.

This is only possible with cooperation from the method that's being
called. Such a method might contain a timeout parameter and check
System.currentTimeMillis() on a regular basis, for example.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

Bent C Dalager

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 :)

Cheers
Bent D
 
K

Knute Johnson

Michael said:
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.

import java.util.*;

public class TimedThread implements Runnable {
Timer timer;
Thread thread;

public TimedThread(int delay) {
timer = new Timer();
TimerTask timerTask = new TimerTask() {
public void run() {
thread.interrupt();
System.out.println("Timed out!");
}
};
timer.schedule(timerTask,delay);

thread = new Thread(this);
thread.start();
}

public void run() {
// this is where your timed code goes
for (int i=0; i<10000000; i++) {
if (Thread.interrupted())
break;
}
timer.cancel();
}

public static void main(String[] args) {
new TimedThread(500);
}
}
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top