I suppose this is kind of an easy question but I'm getting a hard time
with it.
I want to have a loop that breaks upon a normal condition and a time-
condition namely to stop if the other condition haven't been met after
T milliseconds.
I have never used threads and to find how to do this I have read a lot
of info that doesn't seems to apply.
Thank you very much!
rossum - 18 Sep 2007 22:29 GMT
>I suppose this is kind of an easy question but I'm getting a hard time
>with it.
[quoted text clipped - 7 lines]
>
>Thank you very much!
A simple way to do it would be using a volatile variable. You will
also need to chunk up the work you are doing so that you can check the
volatile variable from time to time. Set a second thread running that
just runs down a timer [Thread.sleep()] and changes the volatile
variable when the timer has finished.
No doubt the Java gurus can find a better way than this.
rossum
// --- Begin Code ---
public class TimedLoop {
static volatile boolean timeRunning = true;
static class TimeOut implements Runnable {
private int mDelay;
public TimeOut(int delay) {
mDelay = delay;
} // end constructor
public void run() {
try {
Thread.sleep(mDelay);
} catch (InterruptedException ie) { }
// Flag end of allowed time
timeRunning = false;
} // end run()
} // end class TimeOut
public static void main(String[] args) {
// Start timer thread
Runnable r = new TimeOut(2000);
Thread t = new Thread(r);
t.start();
System.out.println("Starting work loop...");
boolean workFinished = false;
while (timeRunning && !workFinished) {
workFinished = doSomeStuff();
} // end while
if (workFinished) {
System.out.println("Work completed.");
} else {
System.out.println("Loop timed out.");
} // end if
} // end main()
static boolean doSomeStuff() {
boolean workFinished;
// Do a chunk of stuff here
workFinished = false;
//workFinished = true; // For testing
return workFinished;
} // end doSomeStuff()
} // end class TimedLoop
// --- End Code ---
Roedy Green - 18 Sep 2007 23:21 GMT
>I want to have a loop that breaks upon a normal condition and a time-
>condition namely to stop if the other condition haven't been met after
>T milliseconds.
see http://mindprod.com/jgloss/timer.html
When the timer triggers it sets a boolean your main thread checks
periodically. That way it can wrap up gracefully.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Knute Johnson - 18 Sep 2007 23:44 GMT
> I suppose this is kind of an easy question but I'm getting a hard time
> with it.
[quoted text clipped - 7 lines]
>
> Thank you very much!
The others are great examples. I just like this because it is very simple.
public class test7 {
static volatile boolean timesUpFlag;
public static void main(String[] args) {
// create timer thread
Runnable r = new Runnable() {
public void run() {
try {
Thread.sleep(3000);
timesUpFlag = true;
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
};
new Thread(r).start(); // start timer
int x = 0;
// loop until x == Integer.MAX_VALUE or time is up
while (++x < Integer.MAX_VALUE && !timesUpFlag)
;
if (x == Integer.MAX_VALUE)
System.out.println("done");
else
if (timesUpFlag)
System.out.println("timed out x = " + x);
}
}

Signature
Knute Johnson
email s/nospam/knute/
Graham - 19 Sep 2007 09:53 GMT
> I want to have a loop that breaks upon a normal condition and a time-
> condition namely to stop if the other condition haven't been met after
> T milliseconds.
>
> I have never used threads and to find how to do this I have read a lot
> of info that doesn't seems to apply.
A very simple approach would be to check whether the time threshold is
exceeded on every iteration of the loop:
// Time threshold in milliseconds
long timeThreshold = 2000;
// Start time
long startTime = System.currentTimeMillis();
while( !isConditionMet() ) {
// Stop if this is taking too long
if(System.currentTimeMillis() - startTime >= timeThreshold) break;
// Process a bit more
processABitMore();
}
Graham