美文网首页
NSRunLoop--线程

NSRunLoop--线程

作者: 二先生Developer | 来源:发表于2018-01-30 19:11 被阅读46次

NSRunLoop其实本质就是死循环;
作用:Runloop--运行循环

  • 1.保证程序不退出
  • 2.负责监听事件、触摸、时钟、网络事件
    1. 如果没有事件发生,就处于休眠状态

引申一个问题:循环和递归的区别

    1. 递归就是自己调用自己,每调用一次方法压栈一次,相当于在内存开辟了一个空间,每次调用都开辟一个空间,会造成内存空间溢出,递归就结束了,循环没事。
  • 2.递归是不确定循环次数的时候用,而for循环是知道循环次数;

具体使用

1.
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];//虽然苹果帮我们自动加入了运行循环,但是模式是普通模式。就造成处理UI模式式,不能执行普通模式(比如就会造成操作UI,timer停止的问题)

2.手动加入runloop
NSTimer * timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    [[NSRunLoop currentRunLoop] run];
/*
NSRunLoopMode 模式: 
1. 普通模式:NSDefaultRunLoopMode 
2.UITrackingRunLoopMode//UI模式比如拖拽的时候,才会调用timer,松手就不会调用timer事件  
3.占位模式NSRunLoopCommonModes //这个模式可以解决处理UI模型,同时处理普通模式
*/

3. GCD定时器(要注意的是,把dispatch_source_t timer变为全局变量)不然不会执行Block回调
dispatch_queue_t queue = dispatch_get_main_queue();//dispatch_get_global_queue(0, 0)全局队列
     _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW,0.001 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(_timer, ^{
        static NSInteger i =1;
        self.disPlayLabel.text= [ @(6 +i) stringValue];
        i++;
        NSThread * thread =[NSThread currentThread];
        NSLog(@"当前线程===%@",thread);
    });
    dispatch_resume(_timer);

Runloop与线程

//想要保住线程,让线程有执行不完的任务!线程就不会释放了,而不是强引用
 //子线程
    NSThread * thread = [[NSThread alloc] initWithBlock:^{
        NSTimer * timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        
        //Runloop---一条线程上面的runloop默认是不循环,所以执行run函数,才能死循环
        [[NSRunLoop currentRunLoop] run];//死循环
        
        NSLog(@"来了");
    }];
    [thread start];

-(void) timerMethod{
    NSLog(@"come here");
    NSLog(@"timer 来了:%@",[NSThread currentThread]);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"主线程===%@",[NSThread currentThread]);
    [NSThread exit];//干掉主线程,但程序不会崩,其它线程一样可以照常运行,在iOS里面就会出现主线程被干掉,界面卡死
}

相关文章

  • NSRunLoop--线程

    NSRunLoop其实本质就是死循环;作用:Runloop--运行循环 1.保证程序不退出 2.负责监听事件、触摸...

  • Android

    线程间通信 主线程和工作线程主线程和工作线程 工作线程与工作线程工作线程与工作线程 为什么主线程Looper.lo...

  • 三、操作系统之线程

    前言 什么是线程 引入线程的原因 线程的概念 线程和进程的关系 线程结构 线程有点 多线程模型 用户线程和内核线程...

  • Thread

    队列 线程锁 多线程,线程池 队列 多线程爬虫示例 多线程 自定义线程 线程池

  • 总结多线程与设计模式+synchronized+性能+高吞吐+死

    Java线程 Java语言的线程 何谓线程 线程启动 线程的暂时停止 线程的共享互斥 线程的协调 线程的状态转移 ...

  • 多线程编程

    摘要 线程概念,线程与进程的区别与联系学会线程控制,线程创建,线程终止,线程等待了解线程分离与线程安全学会线程同步...

  • java线程池

    线程VS线程池 普通线程使用 创建线程池 执行任务 执行完毕,释放线程对象 线程池 创建线程池 拿线程池线程去执行...

  • java并发之守护线程

    java中有两种线程,用户线程和守护线程用户线程:主线程停止时,用户线程不会停止守护线程:主线程停止时,守护线程也...

  • Java线程池的使用

    线程类型: 固定线程 cached线程 定时线程 固定线程池使用 cache线程池使用 定时调度线程池使用

  • 线程基础知识

    线程学习 线程的基础知识 线程是什么? 线程和进程的关系 线程的6个状态 线程优先级 主线程、多线程、后台线程的概...

网友评论

      本文标题:NSRunLoop--线程

      本文链接:https://www.haomeiwen.com/subject/ydoqzxtx.html