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 correspondingNSRunLoop
object.)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 correspondingNSRunLoop
object.)
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 ...
随机推荐
- SQL注入备忘单
Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet ...
- Oracle自定义函数1
用户定义函数是存储在数据库中的代码块,可以把值返回到调用程序.调用时如同系统函数一样,如max(value)函数,其中,value被称为参数.函数参数有3种类型. IN 参数类型:表示输入给函数的参数 ...
- codevs3031 最富有的人
题目描述 Description 在你的面前有n堆金子,你只能取走其中的两堆,且总价值为这两堆金子的xor值,你想成为最富有的人,你就要有所选择. 输入描述 Input Description 第一行 ...
- P1391 走廊泼水节
时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 话说,中中带领的OIER们打算举行一次冬季泼水节,当然这是要秘密进行的,绝对不可以让中中知道.不过中中可是老 ...
- MyEclipse使用SVN进行项目版本控制
一.搭建SVN服务器. 例如,使用VisualSVN Server,下载后安装. (1)在Repositories(版本库)上右击,新建Repository,选择Regular FSFS reposi ...
- html标签(一)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- iOS @try @catch简单应用举例
- Unable to open liblaunch_sim.dylib. Try reinstalling Xcode or the simulator
关于Xcode7 Beta报错 simulator runtime is not available. Unable to open liblaunch_sim.dylib Try reinstall ...
- Dynamic Virtual Channels
refer http://blogs.msdn.com/b/rds/archive/2007/09/20/dynamic-virtual-channels.aspx An important goal ...
- linux基本命令(4)-8.Ubuntu-jdk+tomcat+eclipse软件包安装
第一步 安装jdk su - root 切换成root用户 sudo -i 不需要密码直接切换成root 1.进入usr目录 cd /usr 2.在usr目录下建立java安装目录 mkdir jav ...