Tuesday, 6 August 2013

Longer running task using schedule timer

Longer running task using schedule timer

import java.util.Timer;
import java.util.TimerTask;
class Test {
boolean in_state;
}
class Processor {
Timer timer;
Test test;
Processor(Test test) {
this.test = test;
init();
}
private init() {
this.timer = new Timer();
this.timer.schedule(new ProcessTask(), 0, 1000/20);
}
private class ProcessTask extends TimerTask {
public void run() {
if(test.in_state) {
return;//skip
}
test.in_state = true;//start flag
if(meetsCondition()) {
//process possibly longer
//make sure next scheduled task execution is delayed/skipped
during until this task execution is finished
}
test.in_state = false;//end flag
}
}
}
What I need to make sure is that the next execution of Process Task
happens when the previous one is finished (which will take longer when a
meetsCondition() occurs).
Thanks for any feedback.

No comments:

Post a Comment