0
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
      }
    }
 }

我需要确保的是,下一次 Process Task 的执行发生在前一个任务完成时(发生 meetCondition() 时会花费更长的时间)。

感谢您的任何反馈。

4

0 回答 0