问题描述
我有一个包含时间段的 Epoch 类。我只被允许在这个时代的时间段内建立一个“东西”(以简化)。这是一个插图:
所以在这个方法中,我想要么返回“东西”,要么等到它成为我的插槽。如果我的插槽时间已经结束,我想等到下一个时代,然后再等待我的插槽。我当前的解决方案适用于 thread.sleep()。像这样:
public Thing generateThing() {
while (true) {
var Now = getTimeStamp();
var activeSlot = epoch.getActiveSlot(Now);
if (!activeSlot.isPresent()) {
this.epoch = buildEpoch();
epoch.awaitMySlot(Now,myaddress); //see wait below
continue;
}
if (!CanIGenerateThing()) {
epoch.awaitMySlot(Now,myaddress); //see wait below
continue;
} else {
return buildThing();
}
}
}
这是等待逻辑:
public void awaitMySlot(long Now,Address self) {
var mySlot = getMySlot(Now,self);
try {
if (mySlot.isPresent()) {
//wait until end of slot
var difference = (mySlot.get().getStart() - Now);
Thread.sleep(difference);
} else {
//wait until end of epoch
var difference = endTimeStamp - Now;
Thread.sleep(difference);
}
} catch (InterruptedException ex) {
ex.printstacktrace();
}
}
如果我理解正确,那么 thread.sleep() 会浪费资源,但我只需要等待我不需要浪费资源。另外我觉得睡眠不是很准确。
在没有 thread.sleep() 的情况下,有没有更好的方法来做到这一点?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)