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 ...
随机推荐
- PowerDesigner-导出表到word
1. 在工具栏中选择[Report -->Reports],如下图 2. 点击第二个图标创建一个Report,如下图 该wizard中有三个信息 Report name Report : Rep ...
- strcmp的实现
注意,*str1++和*str2++最好不要写在while判断里,否则需要在return前再*str1-1,和*str2-1. int strcmp(const char *str1,const ch ...
- 12.Android之Tabhost组件学习
TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...
- BZOJ-2037 Sue的小球 DP+费用提前
似乎很早时学长考过很类似的? 2037: [Sdoi2008]Sue的小球 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 558 Solved: 300 ...
- C++用new和不用new创建类对象区别
new创建类对象,使用完后需使用delete删除,跟申请内存类似.所以,new有时候又不太适合,比如在频繁调用场合,使用局部new类对象就不是个好选择,使用全局类对象或一个经过初始化的全局类指针似乎更 ...
- RSA算法小记
学习来源:http://www.cnblogs.com/vamei/p/3480994.html 小记: 一.数学基础: 欧拉Phi函数:Φ(n)=总数(从1到n-1中与n互质的整数) (1)欧拉定理 ...
- --hdu 1800 Flying to the Mars(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...
- JS 下拉菜单
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- jquery设置和获得checkbox选中问题
1.设置checkbox选中: //选中多选框 checkbox=$("#agentinfo input[name='veri[]']"); //循环多选框中的值 checkbox ...
- PHP定界符 heredoc
<?php echo <<<EOT //如果这个后面有空格,报错... haha EOT; //如果这个后面有空格,报错[如果没有空格,就这样文件直接结束,同样报错,请在EOT ...