iOS中的NSTimer 和 Android 中的Timer
首先看iOS的,
Scheduling Timers in Run Loops
A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer:
Use the
scheduledTimerWithTimeInterval:invocation:repeats:orscheduledTimerWithTimeInterval:target:selector:userInfo:repeats:class method to create the timer and schedule it on the current run loop in the default mode.Use the
timerWithTimeInterval:invocation:repeats:ortimerWithTimeInterval:target:selector:userInfo:repeats:class method to create the timer object without scheduling it on a run loop. (After creating it, you must add the timer to a run loop manually by calling theaddTimer:forMode:method of the correspondingNSRunLoopobject.)Allocate the timer and initialize it using the
initWithFireDate:interval:target:selector:userInfo:repeats:method. (After creating it, you must add the timer to a run loop manually by calling theaddTimer:forMode:method of the correspondingNSRunLoopobject.)
Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.
总结以下,在ios中,一个timer是和一个runloop密切相关的,用timer,就必须设定它的runloop,不然timer是无法正常工作的。另外一个timer invalid之后,就无法再次启用,必须新建timer。
在iOS中,如不采用特殊设置,在应用程序进入后台后,与应用程序相关的线程立即暂停,自然在thread中执行的timer也会停止运行。当应用程序再次进入前台时,暂停的thread会被恢复。
另外需要注意,看下代码
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT , 0), ^{
NSLog(@"async....");
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
[runloop addTimer:timer forMode:NSRunLoopCommonModes];
//注意顺序,先加入源,再用run方法!
[runloop run];
});
新线程如果想启动runloop,不能单单写[runloop run],必须先加入一个触发源,比如这里的timer,不然runloop运行run方法后会立即返回,什么作用都没有。
在Android中,
Timer 在使用时自动开启新线程,比如以下代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
System.out.println("Thread.currentThread()....."+Thread.currentThread().getId()) ;
TimerTask task = new TimerTask() {
public void run() {
//每次需要执行的代码放到这里面。
System.out.println("Thread.currentThread()"+Thread.currentThread().getId()) ;
System.out.println(".........TimerTask.........run");
}
};
Timer timer = new Timer();
timer.schedule(task,1000,1000);
}
以下是输出
Thread.currentThread().....1
Thread.currentThread()319
Thread.currentThread()319
从这里我们可以看出,Android中的timer是自动创建新线程并运行的,主线程的阻塞不会影响定时器的运行。
当程序进入后台后,程序的所有线程都不会停止,直到该线程被系统或代码停止。
iOS中的NSTimer 和 Android 中的Timer的更多相关文章
- ios中的RunLoop 和 android 中的Looper
今天写android程序,用到了Handler,晚上回来查阅资料,发现了Looper这个概念. 看了一下网上关于Looper的资料,发现这个Looper跟ios中的Runloop基本的理念完全一致! ...
- ios中的addChildViewController 和 android中的fragment
刚才突然感觉这2个东西的功能特别像,记录一下,待研究!
- 系统剖析Android中的内存泄漏
[转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...
- Android中插件开发篇之----应用换肤原理解析
一.前言 今天又到周末了,感觉时间过的很快呀.又要写blog了.那么今天就来看看应用的换肤原理解析.在之前的一篇博客中我说道了Android中的插件开发篇的基础:类加载器的相关知识.没看过的同学可以转 ...
- android 中打 Log 的一些技巧
在 android 平台上搞开发工作,会经常用到一些 Log 输出调试信息. 众所周知,android 中有五种类型的 Log , v, d, i, w, e 这里就不再赘 述 (如果对这些不了解的朋 ...
- Android中AppWidget的分析与应用:AppWidgetProvider .
from: http://blog.csdn.net/thl789/article/details/7887968 本文从开发AppWidgetProvider角度出发,看一个AppWidgetPrv ...
- android中的数据库操作(转)
android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库 an ...
- Android中的菜单
本文参考自官方文档:https://developer.android.com/guide/topics/ui/menus.html Android为了维护app之间一个统一的操作习惯,提供了Menu ...
- android中的数据库操作(SQLite)
android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库 an ...
随机推荐
- array,vertor,arraylist,hashable,hashmap等几个易混淆概念的区别
Array可以存放Object和基本数据类型,但创建时必须指定数组的大小,并不能再改变, Vertor是放的Object. Vertor一维,Hashmap/Hashtabe二维: Vertor/Ar ...
- VB中的属性、方法和事件概念解析
Visual Basic 语言中的所有对象都有它们自己的属性.方法和事件,其中包括窗体和控件.可以将属性视为对象的特性,将方法视为对象的操作,而将事件视为对象的响应. 日常生活中的对象(如氦气球)也具 ...
- iOS开发icon&images Size
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix ...
- POJ1947 Rebuilding Roads
Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...
- BZOJ2818 欧拉函数
题意:求1--n中满足gcd(x,y)的值为质数的数对(x,y)的数目 ( (x,y)和(y,x)算两个 ) sol: 设p[i]是一个质数,那么以下两个命题是等价的: 1.gcd(x,y)=1 2. ...
- DLUTOJ #1394 Magic Questions
传送门 Time Limit: 3 Sec Memory Limit: 128 MB Description Alice likes playing games. So she will take ...
- bootstrap学习总结-06 按钮
一按钮的基本样式 Bootstrap提供一组标准的按钮配色和大小调整方案,只需要简单的应用的按钮类即可.BootStrap3提供了按钮的标准样式如图. <!DOCTYPE html> &l ...
- User表格式
"_id":基本是700多 "name":"xx01" "pwd":"123"
- yii增删改查
一.查询数据集合 1.$admin=Admin::model()->findAll($condition,$params);//该方法是根据一个条件查询一个集合,如: findAll( ...
- find只查当前目录 和 -exec和xargs区别
1.find默认查找当前目录和子目录,通过maxdepth限制只查当前目录: find . -maxdepth 1 -type f -name "*.php" 2. find . ...