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. 这里要提醒的是下面这句 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 theACTION_TIME_TICK,ACTION_TIME_CHANGEDandACTION_TIMEZONE_CHANGEDIntentbroadcasts 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), andSystem.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 theuptimeMillis()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)andObject.wait(millis)are always available. These functions use theuptimeMillis()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 withThread.interrupt(), and you must handleInterruptedException.SystemClock.sleep(millis)is a utility function very similar toThread.sleep(millis), but it ignoresInterruptedException. Use this function for delays if you do not useThread.interrupt(), as it will preserve the interrupted state of the thread.The
Handlerclass can schedule asynchronous callbacks at an absolute or relative time. Handler objects also use theuptimeMillis()clock, and require anevent loop(normally present in any GUI application).The
AlarmManagercan trigger one-time or recurring events which occur even when the device is in deep sleep or your application is not running. Events may be scheduled with your choice ofcurrentTimeMillis()(RTC) orelapsedRealtime()(ELAPSED_REALTIME), and cause anIntentbroadcast when they occur.
SystemClock.sleep和Thread.sleep的区别(转)的更多相关文章
- SystemClock.sleep和Thread.sleep的区别
在Java中我们处理线程同步问题时,处理延迟可能会使用Thread类的sleep方法,这里抛开concurrent类的一些方法,其实 Android平台还提供了一个SystemClock.sleep方 ...
- java多线程机制中的Thread和Runnable()区别
1.java语言使用Thread类及其子类对象来表示线程,新建的一个线程声明周期中经历 新建.(声明一个线程,此时他已经有了相应的内存空间和其他资源),运行(线程创建之久就据用了运行的条件,一旦轮到使 ...
- 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别
JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...
- PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...
- android Thread和Runable区别,精讲(有疑问)
网上总是说Runable和Thread可以实现线程,这导致我对Thread和Runable有错误的理解,谁让当时不求甚解,让我一直以为实现Runable可以开启线程. 看过源码后进行区分这两者. 无论 ...
- Android开发:Handler Runnable和Thread之间的区别和联系 应用--------------------看完本篇,从此一览无余!
http://blog.csdn.net/yanzi1225627/article/details/8582081 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnab ...
- java多线程—Runnable、Thread、Callable区别
多线程编程优点 进程之间不能共享内存,但线程之间共享内存非常容易. 系统创建线程所分配的资源相对创建进程而言,代价非常小. Java中实现多线程有3种方法: 继承Thread类 实现Runnable接 ...
- Thread.sleep()和Thread.currentThread().sleep()区别
先看一下代码 public class Thread1 extends Thread{ @Override public void run() { try { System.out.println(& ...
- Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术
package mythread; public class CountOperate extends Thread{ public CountOperate(){ System.out.prin ...
随机推荐
- Swift 集合类型
Swift语言提供数组和字典的集合类型 Swift 语言里的数组和字典中存储的数据值类型必须明确 ,即数组中只能存放同类型的数据. 1: 数组 数组的创建 var shoppingList: St ...
- Untracked files不想add
$ git status On branch feature/20160420_complain_630222 Untracked files: (use "git add <file ...
- 完成端口CreateIoCompletionPort编写高性能的网络模型程序
1.同步网络模型:就是服务端同步阻塞等待客户端的请求,然后继续操作后续处理,缺点是性能低. 2.同步通讯+多线程模型:服务端为每个客户端分配线程,这个线程就负责这个客户端,同步通讯,同步处理这个客户端 ...
- POJ 1013 Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36206 Accepted: 11 ...
- python 字符串比较
cmp方法比较两个对象,并根据结果返回一个整数.cmp(x,y)如果X< Y,返回值是负数 如果X>Y 返回的值为正数. sStr1 = 'strch'sStr2 = 'strchr'pr ...
- TF-IDF 简介
假设我们手头有大量的文档(或网页), 通常我们会比较关心以下几个问题: 1. 每一个文档的关键词(或主题词)包括哪些? 2. 给定一个(或一组)关键词,与这个(或组)词最相关的文档是哪一个? 3. ...
- 基于 Markdown 的开源的 Node.js 知识库平台
Raneto 是一个免费,开源的 Node.js 知识库平台,基于静态 Markdown 文件实现. Raneto 可以被称为静态网站生成器,因为它并不需要数据库支持.所有的内容都存储在 Markdo ...
- python下的orm基本操作(1)--Mysql下的CRUD简单操作(含源码DEMO)
最近逐渐打算将工作的环境转移到ubuntu下,突然发现对于我来说,这ubuntu对于我这种上上网,收收邮件,写写博客,写写程序的时实在是太合适了,除了刚接触的时候会不怎么完全适应命令行及各种权限管理, ...
- 软件工程——sprint 1回顾总结
主题:“我们在本次sprint中做了什么?接下来的打算?” sprint总结:在本次sprint里,这是我们团队的成员们第一次开始以团队的形式进行一次团队项目开发,早在第一次团队会议之时,我们便因团队 ...
- 【.NET框架】Dapper ORM 用法—Net下无敌的ORM
假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载 Dapper的优势: 1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编 ...