在线程的使用过程中会经常用到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();
}
}
本文详细解析了Java中线程sleep方法的具体实现原理。通过分析sleep方法的源代码,阐述了如何设置线程休眠的时间,以及在休眠过程中如何处理被中断的情况。
1674

被折叠的 条评论
为什么被折叠?



