android开发——不会被异常打断的线程休眠SystemClock.sleep()

源码如下:

public static void sleep(long ms)
{
    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) {
        // Important: we don't want to quietly eat an interrupt() event,
        // so we make sure to re-interrupt the thread so that the next
        // call to Thread.sleep() or Object.wait() will be interrupted.
        Thread.currentThread().interrupt();
    }
}

  如果对您有用的话赞一下呗!谢谢谢谢~

你可能感兴趣的:(进阶篇)