参考文档:http://www.cnblogs.com/junhuawang/p/4647559.html
 
- (void)viewDidLoad
{
[super viewDidLoad];
 
dispatch_queue_t queue = dispatch_queue_create("kk", DISPATCH_QUEUE_SERIAL);
 
// 串行队列中执行异步任务
dispatch_async(queue, ^{
 
// 在子线程中使用定时器
 
/*************************************************************/
 
/*
 
// 第一种方式
 
// 此种方式创建的timer已经添加至runloop中
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
 
// 在线程中使用定时器,如果不启动run loop,timer的事件是不会响应的,而子线程中runloop默认没有启动
// 让线程执行一个周期性的任务,如果不启动run loop, 线程跑完就可能被系统释放了
[[NSRunLoop currentRunLoop] run];// 如果没有这句,doSomething将不会执行!!!
 
*/
 
/*************************************************************/
 
 
 
/*************************************************************/
 
// 第二种方式
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(doAnything) userInfo:nil repeats:NO];
 
// 将定时器添加到runloop中
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
 
// 在线程中使用定时器,如果不启动run loop,timer的事件是不会响应的,而子线程中runloop默认没有启动
// 让线程执行一个周期性的任务,如果不启动run loop, 线程跑完就可能被系统释放了
[[NSRunLoop currentRunLoop] run];// 如果没有这句,doAnything将不会执行!!!
 
/*************************************************************/
 
NSLog(@"子线程结束");
 
});
}
 
- (void)doSomething{
 
NSLog(@"doSomething...");
}
 
- (void)doAnything{
 
NSLog(@"doAnything...");
}

优化定时器NSTimer-runloop使用的更多相关文章

  1. iOS UI-Lable标签、NStimer定时器和RunLoop超级死循环

    // 标签UILable -显示文字 // 1.创建标签 UILabel *lable = [[UILabel alloc] init]; // 2.设置标签的坐标和大小 [lable setFram ...

  2. 定时器(NSTimer)

    iOS中定时器NSTimer的使用 1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget sel ...

  3. iOS定时器-- NSTimer 和CADisplaylink

    iOS定时器-- NSTimer 和CADisplaylink 一.iOS中有两种不同的定时器: 1.  NSTimer(时间间隔可以任意设定,最小0.1ms)// If seconds is les ...

  4. 定时器NSTimer的用法

        //时间间隔     NSTimeInterval activeTimeInterval = NETWORK_SEND_ACTIVE_TIME;     NSTimeInterval othe ...

  5. [置顶] ios 时间定时器 NSTimer应用demo

    原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9251917 demo功能:ios NSTimer应用demo .ipho ...

  6. runloop 和 CFRunLoop - 定时器 - NSTimer 和 GCD定时器

    1. 2. #import "ViewController.h" @interface ViewController () @property (nonatomic, strong ...

  7. ios基础篇(二十三)—— 定时器NSTimer与图片的自动切换

    一.NSTimer NSTimer是一个能在从现在开始到后面的某一个时刻或者周期性的执行我们指定的方法的对象.可以按照一定的时间间隔,将制定的信息发送给目标对象.并更新某个对象的行为.你可以选择在未来 ...

  8. IOS中定时器NSTimer的开启与关闭

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  9. iOS定时器NSTimer的使用方法

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

随机推荐

  1. LESS用法·

    CSS 彻底改变了 Web 页面的设计,但 CSS 仍然是静态的,而且在其句法发展方面受到限制.这些限制是有目的且合乎情理的,鼓励广泛加以实现.但开发人员和设计人员常常发现 CSS 使用起来很单调乏味 ...

  2. Eclipse快捷键大全

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  3. CentOS 7 安装 MySQL Database

    CentOS 7 安装 MySQL Database 1. 现在安装包,MySQL的安装包被分成了社区版和企业版,而本文将记录社区版本MySQL安装过程,下载MySQL版本如下: mysql-5.7. ...

  4. VS 打开工程后 自动关闭

    今天在打开一个VS2008的工程的时候,会提示vs2008 已停止工作的异常信息,具体的解决办法如下: 打开vs2008命令提示窗口: 打开窗口后:键入:devenv.exe /resetuserda ...

  5. junit单元测试(keeps the bar green to keeps the code clean)

    error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...

  6. S3C2440的Linux启动过程分析(一)——SC2440处理器结构

    1.1.     S3C2440处理器结构 S3C2440处理器的结构如下图所示,其核心单元为ARM9TDMI处理器核,主要包括16K的指令Cache和16K的数据Cache,以及分开的指令和数据MM ...

  7. 运动规划 (Motion Planning): MoveIt! 与 OMPL

    原创博文:转载请标明出处:http://www.cnblogs.com/zxouxuewei 最近有不少人询问有关MoveIt!与OMPL相关的话题,但是大部分问题都集中于XXX功能怎么实现,XXX错 ...

  8. 用composer安装Yii

    YII2通过Composer安装方法http://www.yiichina.com/download 一.Composer安装首先到https://getcomposer.org/doc/00-int ...

  9. 对tomcat中使用反射加载类的理解

    public void init() throws Exception { initClassLoaders(); //加载一下jar包和类 Thread.currentThread().setCon ...

  10. LeetCode-Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...