参考文档: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. collections系列

    一.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 c = Counter('abcdeabcdabcaba') prin ...

  2. 读取手机上所有应用程序并显示(APP)

    pd = ProgressDialog.show(getActivity(), "请稍候..", "正在收集软件信息...", true,false); Thr ...

  3. background-position

    在学习网页"换肤"效果时:对background-position的理解更深了. 这是我使用的一整张图片:

  4. git学习:关于origin和master

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2016/10/27 本文主要是对这篇博客文章的理解. git的服务器端(remote)端包含多个repository, ...

  5. C++继承,多重继承,虚继承的构造函数以及析构函数的调用顺序问题

    #include <iostream> using namespace std; class A{ int data_a; public: A(){ data_a = ; cout < ...

  6. C#学习手册

    考研学子为何放弃考研?C++开发ArcGis为何无疾而终?C#为何又成为新宠?这一切得一切是人性的扭曲还是道德的败坏,敬请收看接下来的C#学习手册.ps:一天一更.拖更打死.

  7. myeclipse10中文注释乱码问题

    将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclipse编码格式不同. 总结网上的建议和自己的体 ...

  8. Oracle笔记2-数据库设计

    数据库的设计 软件开发的流程:立项->需求分析->概要设计->详细设计->实现->测试->交付->维护 [含数据库设计] 通过需求分析,就可以抽取出关键业务中 ...

  9. Ajax+JQuery

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. Document树的解析方法

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