请详细解释一下这一小段程序

来源:百度知道 编辑:UC知道 时间:2024/05/25 14:09:05
public static void wait1second()
// post: pause for one second
{
long now = System.currentTimeMillis();
long then = now + 1000; // one second of milliseconds
while (System.currentTimeMillis() < then)
{
// do nothing!
}
}

public static void wait1second()
// post: pause for one second

{
long now = System.currentTimeMillis(); // 定义一个long变量now用于存储当前的系统时间
// 再定义一个long变量then来存储当前时间加上1000时间单位(毫秒,也就是1秒)
long then = now + 1000; // one second of milliseconds
// 当前系统时间减去then也就是减去1秒小于0时执行循环
while (System.currentTimeMillis() < then)
{
// do nothing! // 什么也不干,等待!
}
// 1秒后跳出循环体,结束循环
}

你要理解的是System.currentTimeMillis()这个函数所返回的值也就是当前的系统时间是时刻在增加的(除非你的系统烂掉或者时间停止) 。

定义一个公共静态函数,没有返回值
函数名:wait1second()
功能:等一秒钟
函数中调用系统函数:System.currentTimeMillis()
取得当时时间单位毫秒赋值给变量now
再定义变量then为一秒钟以后long then = now + 1000
然后做空循环while (System.currentTimeMillis() < then) 直到现在的时间超过将来的一秒