美文网首页
iOS 从 PerformSelector:onThread:w

iOS 从 PerformSelector:onThread:w

作者: developer_cp3 | 来源:发表于2020-04-01 14:31 被阅读0次

    PerformSelector:onThread:withObject:waitUntilDone

    这个方法的理解就是,在制定线程上执行一个selector

    waitUntilDone : YES, 需要等待这个selector 执行完之后,才会往下执行,相当于同步。

    waitUntilDone : NO, 不用等待这个selector,就会往下执行,相当于异步。仅仅是相当于。

    本来这个方法是用于不同线程之间相互发消息,调用的,但是我测试的时候发现了一个奇葩的东西,直接上代码吧。

    - (void)testFunc {

         dispatch_queue_t queue = dispatch_queue_create("com.drive.com", 0);

        dispatch_async(queue, ^{

            [self performSelector:@selector(recieveMsg) onThread:[NSThread currentThread] withObject:@"123" waitUntilDone:NO];

            NSLog(@"123");

        });

    }

    - (void)recieveMsg {

        NSLog(@"收到消息了,在这个线程:%@",[NSThread currentThread]);

    }

    各位觉得会打印 recieveMsg 中的内容吗?只打印了“123”就结束了

    答案是不会。为什么?

    我的推测:异步,先打印“123”, 由于线程没有加入runLoop,执行完线程就结束,或者处于一个不活跃的状态了(但是线程肯定还在,只是不执行刚才等待异步执行的recieveMsg,否则会crash)。

    怎么验证,在  NSLog(@"123"); 后面加上:

    [[NSRunLoop currentRunLoop]run];

    正常了,打印结果为。”123“,recieveMsg中的内容

    因为加入了runloop,线程一直keepAlive

    这只是我的推测。

    那么我么换一种异步的方式

    - (void)testFunc {

    dispatch_queue_t queue = dispatch_queue_create("com.drive.com", 0);

        dispatch_async(queue, ^{

     dispatch_async(dispatch_get_global_queue(0, 0), ^{

                [selfrecieveMsg];

            });

            NSLog(@"123");

        });

    }

    这样发现还是打印了recieveMsg  中的内容,why?

    只能说明 GCD 和Perform 工作机制不一样,或者GCD 内部做了处理

    我的推测不一定对,如果了解这里面原理的,请告知原因,万分感激!

    到另外一个问题。上代码:

        self.thread = [[NSThread alloc]initWithBlock:^{

            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

        }];

    [self.thread start];

    这是新建了一个线程,并运行runloop,接下来

    [self performSelector:@selector(doStart) onThread:self.thread withObject:nil waitUntilDone:NO];

    - (void)doStart{

        while(!self.isStopped) {

            NSLog(@"while+++");

            NSLog(@"current thread:%@ in while",[NSThread currentThread]);

            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

            NSLog(@"while---");

        }

        NSLog(@"thread end");

    }

    这是做什么,这就是通过while 循环,保持 runloop一直存在,否则可能存在一个问题,就是runloop 没有source 或者timer,就会quit runloop,那么这个到底是怎样工作的呢。

    按照我以前的理解,我在子线程,执行一个while 循环,而且永远卡在循环里,那么我再在这个子线程做什么事,应该会不执行才对:

    [self performSelector:@selector(recieveMsg) onThread:self.thread withObject:nil waitUntilDone:NO];

    但是结果,却是执行了recieveMsg里面的操作,然后继续卡在循环里。怎么理解?

    我的理解是这样,我进入while 之后,通过runLoop 使 该runloop 处于等待,也可以理解为休眠,总之就是还活着,这个时候,我在其他线程通过performSelector ,给子线程发消息,相当于来了一个source0,这个时候,会唤醒runloop,执行source0,接着执行while循环,发现没有达到退出while 循环的条件,又执行runMode方法, 继续休眠。所以是通过其他线程发消息,去唤醒的子线程。

    假如我们做以下修改:

    [self performSelector:@selector(doStart) onThread:self.thread withObject:nil waitUntilDone:YES];

    [self performSelector:@selector(recieveMsg) onThread:self.thread withObject:nil waitUntilDone:YES];

    会发现crash,且永远不会执行到recieveMsg, 为什么,因为我们主线程要等待doStart 执行完,问题是doStart 永远执行不完,他在dowhile中等待着呢。

    通过performSelector 执行的方法,是串行的,因为如果是并行的,那么 我们改下如下方法

    [self performSelector:@selector(doStart) onThread:self.thread withObject:nil waitUntilDone:NO];

    [self performSelector:@selector(recieveMsg) onThread:self.thread withObject:nil waitUntilDone:NO];

    - (void)doStart{

    while(!self.isStopped) {

            NSLog(@"while+++");

            NSLog(@"current thread:%@ in while",[NSThread currentThread]);

            NSLog(@"while---");

        }

        NSLog(@"thread end");

    }

    我们会发现,死循环,且,永远不会执行到recieveMsg,如果是并行的,那么就跟

    dispatch_async(queue,^{...});dispatch_async(queue,^{...});

    一样,两个块里面的代码都会执行,因为是串行的,所以,recieveMsg一定会等到dostart执行完再执行,但是dostart 是死循环,执行不完

    我们可以通过以下来验证, 验证performSelector 是串行还是并行。

      [self performSelector:@selector(testFuncA) onThread:self.thread withObject:nil waitUntilDone:NO];

       [self performSelector:@selector(testFuncB) onThread:self.thread withObject:nil waitUntilDone:NO];

    - (void)testFuncA {

        inti =0;

        while(i<30) {

            NSLog(@"11111");

            i = i +1;

        }

    }

    - (void)testFuncB {

        inti =0;

        while(i<30) {

            NSLog(@"22222");

            i = i +1;

        }

    }

    我们发现,永远都是testFuncA 执行完之后再执行testFuncB,所以一定是串行的。

    同理我们测试下 main_queue 是concurrent_queue 还是DISPATCH_QUEUE_SERIAL

    dispatch_async(dispatch_get_main_queue(), ^{

            inti =0;

            while(i<30) {

                NSLog(@"11111");

                i = i +1;

            }

        });

        dispatch_async(dispatch_get_main_queue(), ^{

            inti =0;

            while(i<30) {

                NSLog(@"22222");

                i = i +1;

            }

        });

    测试结果发现,也是DISPATCH_QUEUE_SERIAL queue。

    总结,我们可以这么理解,

     [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

    只是让子线程runloop一直处于等待中,只要有一个resource 执行了,那么他不会再等了,因为没有resource了(timer 除外),如果我们想让runloop一直等咋办呢,while 循环,执行完resource事件后,再让runloop 等着,直到我们手动控制条件,不让runloop 等待。

    相关文章

      网友评论

          本文标题:iOS 从 PerformSelector:onThread:w

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