使它成为自调度任务。用伪代码:
public class PollingTaskRunner {...CountDownLatch doneWait = new CountDownLatch(1);volatile boolean done;PollingTaskRunner(Runnable pollingTask, int frequency, int period) { ... endTime = now + period; executor.schedule(this, 0);}run() { try { pollingTask.run(); } catch (Exception e) { ... } if (pollingTask.isComplete() || now + frequency > endTime) { done = true; doneWait.countDown(); executor.shutdown(); } else { executor.schedule(this, frequency); }}await() { doneWait.await();}isDone() { return done;}}
它并不复杂,但是在您第一次运行/测试时添加大量调试语句,以便您了解发生了什么。一旦按预期运行,就很容易重用该模式。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)