美文网首页
RunLoop与线程使用实例

RunLoop与线程使用实例

作者: Code_人生 | 来源:发表于2019-09-30 14:01 被阅读0次

1、代码实例1

  • [[NSRunLoop currentRunLoop] run];performSelector之前,不执行test
  • [[NSRunLoop currentRunLoop] run];performSelector之后,执行test

[self performSelector:@selector(test) withObject:nil afterDelay:0];
1、此方法是,在当前runloop中添加了一个timer,用于执行aSelector message
2、会卡住当前线程(只会卡子线程哦,主线程不会卡)

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"1");
        //RunLoop timer
        
//        [[NSRunLoop currentRunLoop] run];//不执行test
        
        //This method sets up a timer to perform the aSelector message on the current thread’s run loop.
        //此方法是,在当前runloop中添加了一个timer,用于执行aSelector message
        [self performSelector:@selector(test) withObject:nil afterDelay:0];//延迟调用会卡住当前线程(只会卡子线程哦,主线程不会卡)
        
        [[NSRunLoop currentRunLoop] run];//执行test
        NSLog(@"3");
    });
}

- (void)test{
    NSLog(@"2");
}

2019-09-18 10:38:37.881686+0800 LGRunLoopTest[1431:396724] 1
2019-09-18 10:38:37.882088+0800 LGRunLoopTest[1431:396724] 2
2019-09-18 10:38:37.882203+0800 LGRunLoopTest[1431:396724] 3

2、代码实例2 子线程timer,[[NSRunLoop currentRunLoop] run];后的代码能否执行

  • timer运行当中,NSRunLoop可以理解为不休眠,所以下面的NSLog(@"3");没法执行
  • repeats改为NONSRunLoop执行完time,然后执行NSLog(@"3");
  • 会卡住当前线程(只会卡子线程哦,主线程不会卡)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"1");
        NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(test) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
        NSLog(@"3");
    });
}

- (void)test{
    NSLog(@"2");
}

2019-09-18 10:48:56.215924+0800 LGRunLoopTest[1484:422074] 1
2019-09-18 10:48:57.220180+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:48:58.219931+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:48:59.220414+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:49:00.216648+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:49:01.218170+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:49:02.219192+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:49:03.220644+0800 LGRunLoopTest[1484:422074] 2
...

3、代码实例3 主线程timer,timer后的代码执行

  • 主线程后面的执行,这个和子线程不一样。
  • 会卡住当前线程(只会卡子线程哦,主线程不会卡)
  • NSRunLoop 运行循环,没有事情的时候休眠,可以理解为可以执行run后面的代码;有事情的时候是个循环,没法执行run后面的代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    NSLog(@"1");
    NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(test) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    NSLog(@"3");
}

- (void)test{
    NSLog(@"2");
}

2019-09-18 10:57:00.964758+0800 LGRunLoopTest[1497:444653] 1
2019-09-18 10:57:00.964953+0800 LGRunLoopTest[1497:444653] 3
2019-09-18 10:57:01.965108+0800 LGRunLoopTest[1497:444653] 2
2019-09-18 10:57:02.965569+0800 LGRunLoopTest[1497:444653] 2
2019-09-18 10:57:03.965910+0800 LGRunLoopTest[1497:444653] 2
2019-09-18 10:57:04.965903+0800 LGRunLoopTest[1497:444653] 2
...

相关文章

  • RunLoop与线程使用实例

    1、代码实例1 [[NSRunLoop currentRunLoop] run];在performSelector...

  • runloop

    runloop是什么 runloop与线程有什么关系 runloop的原理 runloop的使用

  • ZJYiOS学习规划

    一、runloop 1.runloop与线程之间的关系 2.runloop的启动模式与NSTimer使用时候的注意...

  • RunLoop其实没有我们想的那么难

    目录一、纯纯的RunLoop(上小菜)二、RunLoop与多线程相结合使用(上大菜) 一、纯纯的RunLoop(上...

  • RunLoop与线程

    RunLoop与线程之间的关系 每条线程都有唯一一个与之对应的RunLoop对象主线程的RunLoop已经自动创建...

  • NSTimer的循环引用

    NSTimer基本使用 NSTimer与RunLoop NSTimer 循环引用的问题 如何在子线程使用NSTim...

  • Autorelease 对象的内存管理

    AutoreleasePool 与Runloop 关系 主线程默认会开启Runloop, Runloop 会自动帮...

  • 深入理解Runloop

    RunLoop 的概念 RunLoop 与线程的关系 RunLoop 对外的接口 RunLoop 的 Mode R...

  • RunLoop

    一、获取RunLoop 二、RunLoop与线程关系1、每条线程都有唯一的一个与之对应的RunLoop对象2、Ru...

  • iOS面试题-RunLoop

    1. RunLoop和线程的关系 每条线程都有唯一的一个RunLoop对象与之对应的 主线程的RunLoop是自动...

网友评论

      本文标题:RunLoop与线程使用实例

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