简体   繁体   English

Java时间测量不正确

[英]Java time measurement incorrect

I have a java code that runs an endless loop, over and over again, and under certain conditions, I want to insert an additional step / function call into the loop. 我有一个Java代码,它反复运行无休止的循环,在某些情况下,我想在循环中插入一个附加的步骤/函数调用。

I defined these variables outside of the loop: 我在循环外定义了这些变量:

static boolean wait_activate = false; 
static long wait_tmr = -1; 
static int wait_delay = 6; 

Then, in my endless loop, when I want to enable the additional code: 然后,在无限循环中,当我想启用其他代码时:

if (some-condition) { 
    wait_activate = true; 
    System.out.println("activate"); 
    wait_tmr = (System.currentTimeMillis() / 1000L); 
}

And at the beginning of the loop, I have two checks to insert the additional code: 在循环的开始,我进行了两次检查以插入其他代码:

if (wait_activate && (wait_tmr + wait_delay) < (System.currentTimeMillis() / 1000L)) {
    // if wait period isn't over
    do_additional_stuff(); 
}

if (wait_activate && (wait_tmr + wait_delay) > (System.currentTimeMillis() / 1000L)) {
    // wait time is over: 
    System.out.println("Reset time: " + wait_delay); 
    System.out.println("Stored time: " + wait_tmr); 
    System.out.println("Current time: " + (System.currentTimeMillis() / 1000L)); 
    wait_activate = false; 
    wait_tmr = -1; 
}

Now, the time values are all in seconds / unixtime. 现在,时间值全部以秒/ unixtime为单位。 (currentTimeMillis / 1000 = seconds). (currentTimeMillis / 1000 =秒)。 wait_delay is 6. That would mean, the two println s should only be executed about 6 seconds after activating (with wait_activate = true ). wait_delay为6。这意味着,两个println只能在激活后(使用wait_activate = true )大约6秒钟后执行。

However, the output is this: 但是,输出是这样的:

activate
Reset time: 6
Stored time: 946685043
Current time: 946685045

I don't understand why. 我不明白为什么。 It should wait 6 seconds, according to wait_delay . 根据wait_delay ,它应该等待6秒。 However, the difference between the stored time and the current time is only 2 seconds. 但是,存储时间与当前时间之间的差仅为2秒。 Even if I increase the wait_delay. 即使我增加了wait_delay。 Am I doing something wrong? 难道我做错了什么? Is this a bug in Java or in the weird JRE version I (have to) use (running this on LeJOS 0.9.0-beta on a LEGO EV3)? 这是Java还是我(必须)使用的怪异JRE版本中的错误(在LEGO EV3的LeJOS 0.9.0-beta上运行)?

Am I making a complete "beginner mistake" and just don't see it? 我在犯一个完整的“初学者错误”,只是看不到它吗? Or is something else broken there? 还是那里有其他东西坏了?

It looks like the problem is with comparison operators, the first if condition should have > instead of <, while the second if condition should contain < instead of >. 看起来问题出在比较运算符上,第一个if条件应具有>而不是<,而第二个if条件应包含<而不是>。 You may also turn the second "if" condition into an "else" of first "if" condition to avoid any further confusion. 您还可以将第二个“ if”条件转换为第一个“ if”条件的“ else”,以避免进一步的混乱。

You may not want below assignment to happen over & over inside your endless loop while the wait_activate is ON. 当wait_activate为ON时,您可能不希望以下分配在无限循环内反复发生。 Maybe wrapping this assignment in right if condition will help. 如果条件有所帮助,也许可以将这项作业包装在正确的位置。

wait_tmr = (System.currentTimeMillis() / 1000L); wait_tmr =(System.currentTimeMillis()/ 1000L);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM