我正在使用变量
编辑:我认为我应该将其初始化为:
time_of_last_call
作为我的时间起点;因为nanoTime()
可能会给出负值,所以我不能使用0作为初始化time_of_last_call
的时间起点。如果我使用Long.MIN_VALUE
初始化time_of_last_call
,则可能存在溢出问题。有什么建议吗?编辑:我认为我应该将其初始化为:
long time_of_last_call = System.nanoTime()/1000000L;
//long time_of_last_call =Long.MIN_VALUE; //could have overflow issues
//long time_of_last_call =Long.MIN_VALUE/1000000; // could still have overflow issues?
//BEGIN OF CODE WITH A LOOP
// “time_of_last_call” may (or not) be updated:
if (some_condition)
time_of_last_call=System.nanoTime()/1000000L;
if ( ( System.nanoTime()/1000000L - time_of_last_call ) > 10 )
// do something
//END OF CODE WITH A LOOP
System.nanoTime()/1000000L
会更好。 - AndreasnanoTime()
。无论如何,可以删除对1000000L
的除法(假设发布的代码是其整个用法),与10 * 1000000L
或10_000_000L
进行比较。 - user85421