System.currentTimeMillis()怪异问题

WuYL7812 2013-07-31 10:26:46
public void long getDeltaTime()
{
if (mLastTime == 0L)
{
mLastTime = System.currentTimeMillis();
}

long t = System.currentTimeMillis();
mDeltaTime = t - mLastTime;
mLastTime = t;
return mDeltaTime;
}

求距离上次调用这个函数经过的时间.
mLastTime, mLastTime都是long型变量

奇怪的是,偶尔mDeltaTime得到的是负的, 谁知道为什么
...全文
400 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
WuYL7812 2013-07-31
  • 打赏
  • 举报
回复
有可能和多线程有关, mLastTime当然初始化为零0了
顾小林 2013-07-31
  • 打赏
  • 举报
回复
public static long currentTimeMillis () Added in API level 1 Returns the current time in milliseconds since January 1, 1970 00:00:00.0 UTC. This method always returns UTC times, regardless of the system's time zone. This is often called "Unix time" or "epoch time". Use a DateFormat instance to format this time for display to a human. This method shouldn't be used for measuring timeouts or other elapsed time measurements, as changing the system time can affect the results. Use nanoTime() for that. 看一下 组后那段,这个方法不要用于测试超时或者 经过实践,如果更改系统时间,这个结果会受到影响,你可以尝试nanoTime()方法。 另外 猜测啊 是否和多核有关系呢? mLastTime 初始化值 是否为0?
方紫涵 2013-07-31
  • 打赏
  • 举报
回复
重入问题

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧