thread sleepSystemClock.sleep和Thread.sleep的区别

thread sleep  时间:2022-02-21  阅读:()

SystemClock.sleep和Thread.sleep的区别

在Java中我们处理线程同步问题时,处理延迟可能会使用Thread类的sleep方法,这里抛开concurrent类的一些方法,其实 Android平台还提供了一个SystemClock.sleep方法,它们有什么区别呢? 我们每次调用Thread.sleep时可能会出现InterruptedException异常,而SystemClock.sleep方法则不会,在 SDK上有这样的描述,它将会忽略中断异常。 Three different clocks are available, and they should not be confused: System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the work (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes. uptimeMillis() is counted in milliseconds since the system was booted. This clock s when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls), Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic, and is the mended basis for the general purpose interval timing of user interface events, performance measurements, and anything else that does not need to measure elapsed time during device sleep. Most methods that ept a timestamp value expect the uptimeMillis() clock. elapsedRealtime() is counted in milliseconds since the system was booted, including deep sleep. This clock should be used when measuring time intervals that may span periods of system sleep. There are several mechanisms for controlling the timing of events: Standard functions like Thread.sleep(millis) and Object.wait(millis) are always available. These functions use the uptimeMillis() clock; if the device enters sleep, the remainder of the time will be postponed until the device wakes up. These synchronous functions may be interrupted with Thread.interrupt(), and you must handle InterruptedException. SystemClock.sleep(millis) is a utility function very similar to Thread.sleep(millis), but it ignores InterruptedException. Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread.

Budgetvm12核心 16G 500 GB SSD 或者 2 TB SATA 10GB  20 TB  99美金

Budgetvm(原EZ机房),2005年成立的美国老品牌机房,主打美国4个机房(洛杉矶、芝加哥、达拉斯、迈阿密)和日本东京机房的独立服务器和VPS业务,而且不限制流量,默认提供免费的1800G DDoS防御服务,支持IPv6和IPMI,多种免费中文操作系统可供选择,独立服务器主打大硬盘,多硬盘,大内存,用户可以在后台自行安装系统等管理操作!内存可定制升级到1536G,多块硬盘随时加,14TBSA...

印象云七夕促销,所有机器7折销售,美国CERA低至18元/月 年付217元!

印象云,成立于2019年3月的商家,公司注册于中国香港,国人运行。目前主要从事美国CERA机房高防VPS以及香港三网CN2直连VPS和美国洛杉矶GIA三网线路服务器销售。印象云香港三网CN2机房,主要是CN2直连大陆,超低延迟!对于美国CERA机房应该不陌生,主要是做高防服务器产品的,并且此机房对中国大陆支持比较友好,印象云美国高防VPS服务器去程是163直连、三网回程CN2优化,单IP默认给20...

美国200G美国高防服务器16G,800元

美国高防服务器提速啦专业提供美国高防服务器,美国高防服务器租用,美国抗攻击服务器,高防御美国服务器租用等。我们的海外高防服务器带给您坚不可摧的DDoS防护,保障您的业务不受攻击影响。HostEase美国高防服务器位于加州和洛杉矶数据中心,均为国内访问速度最快最稳定的美国抗攻击机房,带给您快速的访问体验。我们的高防服务器配有最高层级的DDoS防护系统,每款抗攻击服务器均拥有免费DDoS防护额度,让您...

thread sleep为你推荐
excel计算公式excel表格如何用公式计算加减乘除混合运算应用雷达雷达在各方面的用途企业资源管理系统企业内部管理系统有哪些chrome系统谷歌Chrome OS可以用来做什么?移动硬盘文件或目录损坏且无法读取移动硬盘文件或目录损坏且无法读取怎么办??点心os点心 OS 用户界面(UI)的设计理念是什么?私服发布站程序怎么开一个私服发布网站?天空软件站官方网站寻各种实用软件下载地址anyradio不用网络收音机怎么下载!普通的申请yy账号诚心求教【如何申请YY账号】请好心人帮我讲解一下,最好祥细点,非常感谢,一定要祥细点实在太感谢了.!
网游服务器租用 最新代理服务器ip 美国主机排名 服务器评测 hawkhost 海外服务器 sub-process 好看的桌面背景图 论坛空间 hnyd 丹弗 毫秒英文 免空 双线主机 购买国外空间 网页提速 沈阳主机托管 东莞服务器托管 服务器防火墙 成都主机托管 更多