在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. 这里要提醒的是下面这句 Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread. 

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 phone network (seesetCurrentTimeMillis(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 stops 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 asThread.sleep(millls)Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic, and is the recommended 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 accept 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:

SystemClock.sleep和Thread.sleep的区别(转)的更多相关文章

  1. SystemClock.sleep和Thread.sleep的区别

    在Java中我们处理线程同步问题时,处理延迟可能会使用Thread类的sleep方法,这里抛开concurrent类的一些方法,其实 Android平台还提供了一个SystemClock.sleep方 ...

  2. java多线程机制中的Thread和Runnable()区别

    1.java语言使用Thread类及其子类对象来表示线程,新建的一个线程声明周期中经历 新建.(声明一个线程,此时他已经有了相应的内存空间和其他资源),运行(线程创建之久就据用了运行的条件,一旦轮到使 ...

  3. 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别

    JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...

  4. PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别

    链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...

  5. android Thread和Runable区别,精讲(有疑问)

    网上总是说Runable和Thread可以实现线程,这导致我对Thread和Runable有错误的理解,谁让当时不求甚解,让我一直以为实现Runable可以开启线程. 看过源码后进行区分这两者. 无论 ...

  6. Android开发:Handler Runnable和Thread之间的区别和联系 应用--------------------看完本篇,从此一览无余!

    http://blog.csdn.net/yanzi1225627/article/details/8582081 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnab ...

  7. java多线程—Runnable、Thread、Callable区别

    多线程编程优点 进程之间不能共享内存,但线程之间共享内存非常容易. 系统创建线程所分配的资源相对创建进程而言,代价非常小. Java中实现多线程有3种方法: 继承Thread类 实现Runnable接 ...

  8. Thread.sleep()和Thread.currentThread().sleep()区别

    先看一下代码 public class Thread1 extends Thread{ @Override public void run() { try { System.out.println(& ...

  9. Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术

      package mythread; public class CountOperate extends Thread{ public CountOperate(){ System.out.prin ...

随机推荐

  1. SQL语句转摘

    http://www.cnblogs.com/Olive116/p/3271706.html 收藏没有用,来收到留链接

  2. ios 关于文件操作 获取 文件大小

     分类: Apple IPhone2012-06-28 11:31 4664人阅读 评论(0) 收藏 举报 ios语言manager测试c c语言 实现 #include "sys/stat ...

  3. 浅析MongoDB数据库的海量数据存储应用

    [摘要]当今已进入大数据时代,特别是大规模互联网web2.0应用不断发展及云计算所需要的海量存储和海量计算发展,传统的关系型数据库已无法满足这方面的需求.随着NoSQL数据库的不断发展和成熟,可以较好 ...

  4. Sql Server 系统表功能

    SELECT Name FROM Master..SysDatabases ORDER BY Name

  5. Weblogic 10.3.6生产模式启动

    生产模式启动里需要输入用户名和密码,很麻烦.在域的/security目录下创建文件boot.properties,内容为: username=weblogic password=weblogic123 ...

  6. STL中vector小结

    ()使用vector之前必须包含头文件<vector>:#include<vector> ()namespace std{ template <class T, clas ...

  7. Communication - 03.RILC

    RIL层的作用大体上就是将上层的命令转换成相应的AT指令,控制modem工作.生产modem的厂家有很多:Qualcomm, STE, Infineon... 不同的厂家都有各自的特点,当然也会有各自 ...

  8. 解决 ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)异常

    ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510) 解决方案: 在java_opts="-Xd ...

  9. 『摄影欣赏』16幅 Romantic 风格照片欣赏【组图】

    今天,我们将继续分享人类情感的系列文章.爱是人类最重要的感觉,也可能是各种形式的艺术(电影,音乐,书,画等)最常表达的主题 .这里有40个最美丽的爱的照片,将激励和给你一个全新的视觉角度为这种情绪.我 ...

  10. iOS-nil,Nil,NULL的区别

    一.简述 1.nil用来给对象赋值(Objective-C中的任何对象都属于id类型) 2.NULL则给任何指针赋值,NULL和nil不能互换 3.nil用于类指针赋值(在Objective-C中类是 ...