使用计划计时器运行更长的任务

时间:2013-08-06 15:11:34

标签: java 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
      }
    }
 }

我需要确保下一个Process Task的执行发生在前一个执行完成时(当meetCondition()发生时需要更长的时间)。

感谢您的反馈。

0 个答案:

没有答案