首先看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:

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的更多相关文章

  1. ios中的RunLoop 和 android 中的Looper

    今天写android程序,用到了Handler,晚上回来查阅资料,发现了Looper这个概念. 看了一下网上关于Looper的资料,发现这个Looper跟ios中的Runloop基本的理念完全一致! ...

  2. ios中的addChildViewController 和 android中的fragment

    刚才突然感觉这2个东西的功能特别像,记录一下,待研究!

  3. 系统剖析Android中的内存泄漏

    [转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...

  4. Android中插件开发篇之----应用换肤原理解析

    一.前言 今天又到周末了,感觉时间过的很快呀.又要写blog了.那么今天就来看看应用的换肤原理解析.在之前的一篇博客中我说道了Android中的插件开发篇的基础:类加载器的相关知识.没看过的同学可以转 ...

  5. android 中打 Log 的一些技巧

    在 android 平台上搞开发工作,会经常用到一些 Log 输出调试信息. 众所周知,android 中有五种类型的 Log , v, d, i, w, e 这里就不再赘 述 (如果对这些不了解的朋 ...

  6. Android中AppWidget的分析与应用:AppWidgetProvider .

    from: http://blog.csdn.net/thl789/article/details/7887968 本文从开发AppWidgetProvider角度出发,看一个AppWidgetPrv ...

  7. android中的数据库操作(转)

    android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库   an ...

  8. Android中的菜单

    本文参考自官方文档:https://developer.android.com/guide/topics/ui/menus.html Android为了维护app之间一个统一的操作习惯,提供了Menu ...

  9. android中的数据库操作(SQLite)

    android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库   an ...

随机推荐

  1. .NET Core 在Visual Studio 2015 下的使用-MSDN

    .NET Core RC2 现已推出,这是真正的"候选发布"而非 RC1 Beta 冒充的候选发布(如果是那样,请考虑发布后出现的所有更改).当前,围绕 .NET Core 的开发 ...

  2. POJ3038 Flying Right

    Description Figuring that they cannot do worse than the humans have, Farmer John's cows have decided ...

  3. POJ3714 Raid

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10625   Accepted: 3192 Description ...

  4. Redis Installation、Configuration、Program Based On Redis Learning

    目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...

  5. 802.11协议帧格式、Wi-Fi连接交互过程、无线破解入门研究

    相关学习资料 Linux黑客大曝光: 第8章 无线网络 无线网络安全攻防实战进阶 无线网络安全 黑客大曝光 第2版 http://zh.wikipedia.org/wiki/IEEE_802.11 h ...

  6. foeach集合遍历

    package number; public class Number { public static void main(String[] args) { int[] arr={5,2,1,0,3, ...

  7. P1067Warcraft III 守望者的烦恼(十大矩阵问题之七求递推式)

    https://vijos.org/p/1067 守望者-warden,长期在暗夜精灵的的首都艾萨琳内担任视察监狱的任务,监狱是成长条行的,守望者warden拥有一个技能名叫“闪烁”,这个技能可以把她 ...

  8. hdu 2045 不容易系列之(3)—— LELE的RPG难题

    解题思路: f(n)=1,2,.....n-2,n-1,n 前n-2个已经涂好,那么n-1有两种可能 1.n-1与n-2和1 的颜色都不同 1 粉,   n-2 红,   n-1 绿.  那么n的颜色 ...

  9. 锋利的jQuery-3--$()创建节点

    创建节点可以用jquery的工厂函数,$() $() 会根据传入的html标记字符串,创建一个dom对象,并将这个dom对象包装成一个jquery对象后返回. var li_1 = $("& ...

  10. dedecms最新版本修改任意管理员漏洞

    此漏洞无视gpc转义,过80sec注入防御. 补充下,不用担心后台找不到.这只是一个demo,都能修改任意数据库了,还怕拿不到SHELL? 起因是全局变量$GLOBALS可以被任意修改,随便看了下,漏 ...