zoukankan      html  css  js  c++  java
  • 子线程里调用performSelector需要注意什么

    以下代码执行顺序是什么 ?

    - (void)action {

        NSLog(@"1");

        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

        dispatch_async(queue, ^{

            NSLog(@"3");

            [self performSelector:@selector(test) withObject:nil afterDelay:0.0f];

            NSLog(@"4");

        });

        NSLog(@"2");

    }

     

    - (void)test {

        NSLog(@"5");

    }

    结果

    2019-07-02 09:24:18.492671+0800 ModelTest[12945:510979] 1

    2019-07-02 09:24:18.492776+0800 ModelTest[12945:510979] 2

    2019-07-02 09:24:18.492807+0800 ModelTest[12945:511386] 3

    2019-07-02 09:24:18.493009+0800 ModelTest[12945:511386] 4

    test方法没有调用。因为子线程里runloop默认是关闭的。改为一下代码后

    - (void)action {

        NSLog(@"1");

        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

        dispatch_async(queue, ^{

            NSLog(@"3");

            [self performSelector:@selector(test) withObject:nil afterDelay:0.0f];

            [[NSRunLoop currentRunLoop]run];

            NSLog(@"4");

        });

        NSLog(@"2");

    }

     

    - (void)test {

        NSLog(@"5");

    }

    结果

    2019-07-02 09:38:40.874213+0800 ModelTest[13094:524707] 1

    2019-07-02 09:38:40.874530+0800 ModelTest[13094:524707] 2

    2019-07-02 09:38:40.874551+0800 ModelTest[13094:524802] 3

    2019-07-02 09:38:43.792863+0800 ModelTest[13094:524802] 5

    2019-07-02 09:38:43.793109+0800 ModelTest[13094:524802] 4

  • 相关阅读:
    php删除最后一个字符
    git删除远程分支
    lsof命令
    高效率的全组合算法(Java版实现)
    Java类变量和成员变量初始化过程
    pig命令行快捷键
    java的HashCode方法
    待学习
    长连接和短连接
    Hadoop学习之SecondaryNameNode
  • 原文地址:https://www.cnblogs.com/huangzs/p/11118584.html
Copyright © 2011-2022 走看看