参考文档: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. Caffe 源碼閱讀(六) InternalThread

    类InternalThread是一个虚类,是Caffe中的多线程接口,其本质是为封装了boost::thread 看源码可以得到以下结论: 1.每个派生类都需要实现一个InternalThreadEn ...

  2. setInterval和setTimeout

    setTimeout和setInterval的基本用法我们不谈,无非是1.指定延迟后调用函数,2.以指定周期调用函数 让我们想象一个意外情况,比如说下面的setInterval setInterval ...

  3. runtime 初入

    一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C语言,函数的调用在编译的时候会决定调用哪个函数. 对于OC的函数,属于 ...

  4. Linux学习之路—Linux的历史发展

    1.1969年以前:一个伟大的梦想-Bell,MIT与GE的"Multics"系统 由于早期的计算机不想现在这样普遍,所以往往出现这样的情况,主机只有一台而多人等待使用.好在20世 ...

  5. jquery跨域Ajax请求

    sonp原理: 首先在客户端注册一个callback, 然后把callback的名字传给服务器. 此时,服务器先生成 json 数据.然后以 javascript 语法的方式,生成一个function ...

  6. 瀑布流布局(jq实现)

    参考刘晓帆大神的瀑布流效果 自己也写了个 希望能给打算写这个效果的朋友一点点思路,也希望朋友们批评指正! html代码: <!DOCTYPE html> <html> < ...

  7. VTK的学习资源

    本文介绍从哪儿开始学习VTK(Visualization Toolkit的简称),如何在网上找寻VTK的学习资源. 首先,可以到维基百科或者百度百科上查看VTK条目,了解VTK是什么. http:// ...

  8. webStorm 多个项目并存

    点击file ->settings ->Directories 然后右侧有个Add Content Root 添加某个Root即可看见该root下的所有项目

  9. Document树的解析方法

    一.本次总结用到的xml文本 1.    <?xml version="1.0" encoding="UTF-8" standalone="no ...

  10. div各种距离 详细解释图

    详细博文介绍:http://blog.csdn.net/fswan/article/details/17238933