SystemClock.sleep和Thread.sleep的区别
展开全部
在Java中我们处理线程同步问题时,处理延迟可能会使用Thread类的sleep方法,这里抛开concurrent类的一些方法,其实 Android平台还提供了一个SystemClock.sleep方法,它们有什么区别呢?
我们每次调用Thread.sleep时可能会出现InterruptedException异常,而SystemClock.sleep方法则不会,在 SDK上有这样的描述,它将会忽略中断异常。SystemClock.sleep(millis) is a utility function very similar to Thread.sleep(millis), but it ignores InterruptedException. 这里Android开发网要提醒的是下面这句 Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread.
我们每次调用Thread.sleep时可能会出现InterruptedException异常,而SystemClock.sleep方法则不会,在 SDK上有这样的描述,它将会忽略中断异常。SystemClock.sleep(millis) is a utility function very similar to Thread.sleep(millis), but it ignores InterruptedException. 这里Android开发网要提醒的是下面这句 Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询