Hi,
How could I only allow a Java iterative statement to run for only a certain
period. Such as running a while loop for 10 seconds...
Thanks

Signature
Peter Wong
peter@peterwong.org
http://www.peterwong.org
Chris Smith - 19 Apr 2004 19:21 GMT
> How could I only allow a Java iterative statement to run for only a certain
> period. Such as running a while loop for 10 seconds...
long startTime = System.currentTimeMillis();
long period = 10000; // 10 seconds = 10000 millis
while (otherCondition
&& (System.currentTimeMillis() < startTime + period))
{
...
}

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Matt Humphrey - 19 Apr 2004 19:24 GMT
> Hi,
>
> How could I only allow a Java iterative statement to run for only a certain
> period. Such as running a while loop for 10 seconds...
long finishTime= System.currentTimeMillis () + 10 * 1000;
while ( (your condition) && (System.currentTimeMillis () < finishTime) ) {
// Do your stuff
}
if (your conditon) {
it finished ok
} else {
it timed out
}
This cannot be more accurate than the underlying time base, the fairness of
the scheduler and the amount of time used by each loop iteration. If each
iteration is long you may want to consider using a Thread and having the
iteration poll a stop flag at a wide variety of points. Or checkout
thread.join (time).
Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/