Chronometer

G

gaijinco

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!
 
R

rossum

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!

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 ---
 
K

Knute Johnson

gaijinco said:
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!

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

}
}
 
G

Graham

gaijinco said:
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
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top