ios开发整理程序员好东西

iOS深入理解GCD 第一篇( 并行、串行、异步、同步的加深理解

2017-09-08  本文已影响39人  镜花水月cy

并行、串行、异步、同步的加深理解

单个理解:

并行:就是队列里面的任务(代码块,block)不是一个个执行,而是同时执行,不存在先后顺序

串行:队列里面的任务一个接着一个执行,第一个执行完了才能轮到第二个

异步:具有新开线程的能力

同步:不具备有新开线程的能力,只能在当前线程执行任务(所以任务只能一个挨着一个执行,而不能同时进行)

串起来理解:

并行+异步:就是真正的并发,新开有有多个线程处理任务,任务并发执行(不按顺序执行)
串行+异步:新开一个线程,任务一个接一个执行,上一个任务处理完毕,下一个任务才可以被执行
并行+同步:不新开线程,任务一个接一个执行
串行+同步:不新开线程,任务一个接一个执行

​ 这里不得不说一下,之前看了这篇博客

http://www.cnblogs.com/ziyi--caolu/p/4900650.html 该作者成功的将我绕进了里面,其实归根究底,只要是在同步环境中,并行和串行确实就可以这么理解,

即并行+同步 和 串行+同步 都是不新开线程,并且任务一个接一个执行

下面是具体代码示例:

一:异步并行队列内回调同步串行队列

- (void)exampleFirst {
    dispatch_queue_t queue = dispatch_queue_create("com.hao123.www", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---11  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            //打印结果与主队列bingxingyibuqiantaozhu函数保持一致
            NSLog(@"11  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---22  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"22  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---33  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"33  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---44  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"44  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---55  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"55  %@",[NSThread currentThread]);
        });
    });
}

打印结果

2017-09-08 14:29:24.407 GcdInsightTest[63582:2978456] 外部---44  <NSThread: 0x608000072780>{number = 6, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978364] 外部---11  <NSThread: 0x608000070f40>{number = 3, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978366] 外部---33  <NSThread: 0x600000076380>{number = 5, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978363] 外部---22  <NSThread: 0x600000072f00>{number = 4, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978457] 外部---55  <NSThread: 0x608000073340>{number = 7, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978456] 44  <NSThread: 0x608000072780>{number = 6, name = (null)}
2017-09-08 14:29:24.407 GcdInsightTest[63582:2978364] 11  <NSThread: 0x608000070f40>{number = 3, name = (null)}
2017-09-08 14:29:24.408 GcdInsightTest[63582:2978366] 33  <NSThread: 0x600000076380>{number = 5, name = (null)}
2017-09-08 14:29:24.408 GcdInsightTest[63582:2978363] 22  <NSThread: 0x600000072f00>{number = 4, name = (null)}
2017-09-08 14:29:24.408 GcdInsightTest[63582:2978457] 55  <NSThread: 0x608000073340>{number = 7, name = (null)}

二:异步并行内回调同步并行队列

- (void)exampleSecond {
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---11  %@",[NSThread currentThread]);
        //对比异步并行回调同步串行,可以看出当外部异步并行队列未执行完毕时,有可能内部同步并行队列已经开始执行了
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"11  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---22  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"22  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---33  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"33  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---44  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"44  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---55  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"55  %@",[NSThread currentThread]);
        });
    });
}

打印结果

2017-09-08 14:34:59.192 GcdInsightTest[64242:2984344] 外部---11  <NSThread: 0x608000263040>{number = 13, name = (null)}
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984353] 外部---22  <NSThread: 0x608000262480>{number = 17, name = (null)}
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984111] 外部---33  <NSThread: 0x608000073980>{number = 12, name = (null)}
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984345] 外部---55  <NSThread: 0x60000007f1c0>{number = 16, name = (null)}
2017-09-08 14:34:59.192 GcdInsightTest[64242:2984346] 外部---44  <NSThread: 0x608000074d40>{number = 14, name = (null)}
2017-09-08 14:34:59.193 GcdInsightTest[64242:2984344] 11  <NSThread: 0x608000263040>{number = 13, name = (null)}
2017-09-08 14:34:59.194 GcdInsightTest[64242:2984353] 22  <NSThread: 0x608000262480>{number = 17, name = (null)}
2017-09-08 14:34:59.194 GcdInsightTest[64242:2984111] 33  <NSThread: 0x608000073980>{number = 12, name = (null)}
2017-09-08 14:34:59.194 GcdInsightTest[64242:2984345] 55  <NSThread: 0x60000007f1c0>{number = 16, name = (null)}
2017-09-08 14:34:59.194 GcdInsightTest[64242:2984346] 44  <NSThread: 0x608000074d40>{number = 14, name = (null)}

根据两段例子的打印结果,可以显而易见的得出结论:

一:由于外部是异步并行队列,所以外部打印结果不按顺序执行。

二:当外部所有队列执行完毕,内部队列才开始执行。

三:其内部的队列将与外部队列顺序保持一致,无论异步并行队列内回调的是同步串行队列,还是同步并行队列,只有当外部队列执行完毕,接下来内部队列才会在同一个线程继续执行直到任务结束。

根据第三条可以得出结论:
并行+同步:不新开线程,任务一个接一个执行
串行+同步:不新开线程,任务一个接一个执行
这两句话没毛病~

上一篇下一篇

猜你喜欢

热点阅读