在线程的使用过程中会经常用到sleep()函数,其具体实现如下,
//ms为需要休眠的时长
public static void sleep(long ms)
{
//uptimeMillis() Returns milliseconds since boot, not counting time spent in deep sleep.
long start = uptimeMillis();
long duration = ms;
boolean interrupted = false;
do {
try {
Thread.sleep(duration);
}
catch (InterruptedException e) {
interrupted = true;
}
duration = start + ms - uptimeMillis();
} while (duration > 0);
if (interrupted) {
Thread.currentThread().interrupt();
}
}