完整代码:https://github.com/Sheldor5/JavaGPP(见测试两个选项)Java - 太快无法识别?
我有命令模式和循环一些奇怪的行为(递减计数,直到应执行的实际命令动作):
我试图实现:
我目前正在开发一款游戏,在这个游戏中我想放置一个“炸弹”,这个“炸弹”应该在例如2秒。我这样做的命令模式,其中一种类型的命令减去timeLeft
变量的delta
直到timeLeft
变量为< =零 - >如果2秒过去了,爆炸应该发生。如果我在while循环中执行命令,这是行不通的,因为炸弹在+2.2秒后爆炸,而不是在2(和一些毫微秒)之后爆炸。如果我在循环中添加Thread.sleep(1)
,那么对我来说时间就足够准确,所以我可以说delta
的计算已经完成了。
选项1:
// infinite loop: execute all commmands in the executor (only one command for now)
while (true) {
commandExecutor.executeCommands();
}
// the only one command in the executor gets called everytime
public static final long NANOS = 1000000000;
private long timeLeft = NANOS;
private long start;
public Command() {
this.start = System.nanoTime();
}
public execute(final long delta) {
this.timeLeft -= delta;
if (this.timeLeft <= 0) {
final long time = System.nanoTime() - this.start;
System.out.println(String.format("Successfully executed Command with ID '%s' in %dns (%dns)", this.getID(), time, this.timeLeft));
this.timeLeft += NANOS;
}
}
预期输出(1秒间隔):
成功地与ID在1000000454ns '1' 执行的命令(-454ns)
在1000000695ns(-695ns)内成功执行ID为1的命令
成功地与ID在1000000549ns '1' 执行的命令(-549ns)
成功地与ID在1000000003ns '1' 执行的命令(-3ns)
实际输出():
成功执行的命令ID为在1267062727ns '1'(-266ns)
成功地与ID在1350811695 '1' 执行的命令纳秒(-165ns)
成功地与ID在1352353549ns '1' 执行的命令(-145ns)
成功地与ID在1263098003ns '1' 执行的命令(-75ns)
但是,如果我添加Thread.sleep()
进入循环的实际输出是精确得多:
选项2:
while (true) {
commandExecutor.executeCommands();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
可接受的输出:
成功地与ID在1000015964ns '40' 执行Command4(-47338ns)
成功地与ID在1001095472ns '40' 执行Command4(-316309ns)
在1000039457ns(-224116ns)内成功执行ID为40的Command4
在1001043666ns(-662982ns)中成功执行ID为40的Command4(-662982ns)
有人可以向我解释这一点吗?循环太快了吗?速度如此之快以至于无法识别变量的实际值,并且错误地认为它大于零?为什么有这么大的差异,从那里〜0.2秒?
很难跟踪你实际尝试做什么。你能详细说明吗? – Thomas
我将更新问题 – Pali
为什么您只在使用公式中的'this.start' *后已经在公式中使用?那应该是下一个命令的开始时间吗? – Siguza