美文网首页
iOS GCD简单嵌套测试结论

iOS GCD简单嵌套测试结论

作者: MissSaturday | 来源:发表于2022-02-22 18:43 被阅读0次

前言:最近在做SDK方向,对于线程、锁、高并发等关键词使用很频繁。做这个嵌套测试,主要是搞懂代码的执行顺序,从而对线程、队列、任务有更深的理解。

先上结果

image.png

开始测试,默认认为读者对GCD有所了解

临界区:gcd的block里的任务,如果嵌套了gcd,那么其余的作用域,我定义为临界区。

一、当前串行队列

self.cxqueue = dispatch_queue_create("cxqueue", DISPATCH_QUEUE_SERIAL);

1. 在当前队列追加同步任务,死锁

//注意是追加任务 是同一个队列 这种类型必定死锁。比如在主线程追加同步任务会死锁,主线程并不特殊,就是个串行队列。
     dispatch_async(self.cxqueue, ^{
        //不管外边是async还是sync,内部这样嵌套的,必定死锁
        dispatch_sync(self.cxqueue, ^{
            
        });
    });
    
    //典型死锁,属于上边死锁类型
    dispatch_sync(dispatch_get_main_queue(), ^{
         
    });

2. 在当前队列追加异步任务,先执行完临界区任务,再执行追加的任务,不开辟新线程

    dispatch_async(self.cxqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);

        dispatch_async(self.cxqueue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3   - %@",[NSThread currentThread]);
        });
        
        dispatch_async(self.cxqueue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"4   - %@",[NSThread currentThread]);
        });

        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
    });
    
打印  3、4会等待临界区执行完才执行
22:56.16  1 任务开始 - <NSThread: >{number = 6, name = (null)}
22:56.16  2 任务结尾 - <NSThread: >{number = 6, name = (null)}
22:56.66  3   - <NSThread: >{number = 6, name = (null)}
22:57.16  4   - <NSThread: >{number = 6, name = (null)}
    

3. 在当前队列开启同步串行队列,临界区和同步任务在当前线程顺序执行,不开辟新线程

dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(self.cxqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);

        dispatch_sync(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 同步串行 - %@",[NSThread currentThread]);
        });
        
        dispatch_sync(queue, ^{
            //[NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 同步串行 - %@",[NSThread currentThread]);
        });
        
        //[NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
    
打印   临界区2会等待3、4执行完后才执行
43:26.02 1 任务开始 - <NSThread: >{number = 5, name = (null)}
43:26.52 3 同步串行 - <NSThread: >{number = 5, name = (null)}
43:26.53 4 同步串行 - <NSThread: >{number = 5, name = (null)}
43:26.53 2 任务结尾 - <NSThread: >{number = 5, name = (null)}

4. 在当前队列开启异步串行队列,两者没有明确的先后顺序(异步任务之前的代码先执行,后边的谁抢到谁执行),新队列任务按加入顺序顺序执行,开启一个新线程

  dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(self.cxqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);

        dispatch_async(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 异步串行 - %@",[NSThread currentThread]);
        });
        
        dispatch_async(queue, ^{
            //[NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 异步串行 - %@",[NSThread currentThread]);
        });
        
        //[NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印   
任务3 会等待 临界区1执行完
任务4 会等待 任务3执行完
临界区2任务 不会等待3、4执行完,但是肯定在1后边执行
40:02.56  1 任务开始 - <NSThread: >{number = 7, name = (null)}
40:02.56  2 任务结尾 - <NSThread: >{number = 7, name = (null)}
40:03.06  3 异步串行 - <NSThread: >{number = 5, name = (null)}
40:03.06  4 异步串行 - <NSThread: >{number = 5, name = (null)}

5. 在当前队列开启同步并发队列,临界区和同步任务在当前线程顺序执行,不开辟新线程

 dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(self.cxqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:0.5];
        dispatch_sync(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 同步并发 - %@",[NSThread currentThread]);
        });
        
        dispatch_sync(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 同步并发 - %@",[NSThread currentThread]);
        });
        
        [NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印   3等1 4等3  2等4
33:40.64 1 任务开始 - <NSThread: >{number = 6, name = (null)}
33:41.65 3 同步并发 - <NSThread: >{number = 6, name = (null)}
33:42.15 4 同步并发 - <NSThread: >{number = 6, name = (null)}
33:43.16 2 任务结尾 - <NSThread: >{number = 6, name = (null)}

6. 在当前队列开启异步并发队列,两者没有明确的先后顺序(异步任务之前的代码先执行,后边的谁抢到谁执行),新队列任务并发执行,开启多个新线程

dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(self.cxqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:0.5];
        
        dispatch_async(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 异步并发 - %@",[NSThread currentThread]);
        });
        
        dispatch_async(queue, ^{
            //[NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 异步并发 - %@",[NSThread currentThread]);
        });
        
        //[NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印
234均等待1部分执行完毕
234互相之间无关联,谁先抢到资源谁就调用
把串行队列想象成主队列,就相当于在主线程开了异步并发队列
37:42.60 1 任务开始 - <NSThread:>{number = 6, name = (null)}
37:43.11 2 任务结尾 - <NSThread:>{number = 6, name = (null)}
37:43.11 4 异步并发 - <NSThread:>{number = 4, name = (null)}
37:43.61 3 异步并发 - <NSThread:>{number = 7, name = (null)}    

二、当前并发队列(同步异步结论一样)

self.bfqueue = dispatch_queue_create("bfqueue", DISPATCH_QUEUE_CONCURRENT);

1. 在当前队列追加同步任务,临界区和同步任务在当前线程顺序执行,不开辟新线程

    dispatch_async(self.bfqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:0.5];
        
        dispatch_sync(self.bfqueue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 同步并发 - %@",[NSThread currentThread]);
        });
        
        dispatch_sync(self.bfqueue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 同步并发 - %@",[NSThread currentThread]);
        });
        
        [NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印
48:51.16 1 任务开始 - <NSThread:>{number = 7, name = (null)}
48:52.17 3 同步并发 - <NSThread:>{number = 7, name = (null)}
48:52.67 4 同步并发 - <NSThread:>{number = 7, name = (null)}
48:53.67 2 任务结尾 - <NSThread:>{number = 7, name = (null)}

2. 在当前队列追加异步任务,任务无序执行,开辟多个线程

    dispatch_async(self.bfqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:0.5];
        
        dispatch_async(self.bfqueue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 异步并发 - %@",[NSThread currentThread]);
        });
        
        dispatch_async(self.bfqueue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 异步并发 - %@",[NSThread currentThread]);
        });
        
        //[NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印 234必定等1部分执行完  234无序
52:03.50 1 任务开始 - <NSThread:>{number = 5, name = (null)}
52:04.00 2 任务结尾 - <NSThread:>{number = 5, name = (null)}
52:04.51 4 异步并发 - <NSThread:>{number = 7, name = (null)}
52:04.51 3 异步并发 - <NSThread:>{number = 6, name = (null)}

3. 在当前队列开启同步串行队列,临界区和同步任务在当前线程顺序执行,不开辟新线程

dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(self.bfqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:1];
        
        dispatch_sync(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 同步串行 - %@",[NSThread currentThread]);
        });
        
        dispatch_sync(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 同步串行 - %@",[NSThread currentThread]);
        });
        
        //[NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印   顺序等待
02:38.89 1 任务开始 - <NSThread:>{number = 6, name = (null)}
02:40.40 3 同步串行 - <NSThread:>{number = 6, name = (null)}
02:40.90 4 同步串行 - <NSThread:>{number = 6, name = (null)}
02:40.90 2 任务结尾 - <NSThread:>{number = 6, name = (null)}

4. 在当前队列开启异步串行队列,两者没有明确的先后顺序(异步任务之前的代码先执行,后边的谁抢到谁执行),新队列任务按加入顺序顺序执行,开启一个新线程

dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(self.bfqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:1];
        
        dispatch_async(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 异步串行 - %@",[NSThread currentThread]);
            [NSThread sleepForTimeInterval:0.5];
        });
        
        dispatch_async(queue, ^{
            //[NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 异步串行 - %@",[NSThread currentThread]);
        });
        
        //[NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印
234必定等1部分结束
23无序,4必定等3结束
05:44.06 1 任务开始 - <NSThread:>{number = 6, name = (null)}
05:45.07 2 任务结尾 - <NSThread:>{number = 6, name = (null)}
05:45.57 3 异步串行 - <NSThread:>{number = 4, name = (null)}
05:46.08 4 异步串行 - <NSThread:>{number = 4, name = (null)}

5. 在当前队列开启同步并发队列,临界区和同步任务在当前线程顺序执行,不开辟新线程

dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(self.bfqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:1];
        
        dispatch_sync(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 同步并发 - %@",[NSThread currentThread]);
            [NSThread sleepForTimeInterval:0.5];
        });
        
        dispatch_sync(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 同步并发 - %@",[NSThread currentThread]);
        });
        
        //[NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印 3等1 4等3  2等4
11:39.67 1 任务开始 - <NSThread:>{number = 6, name = (null)}
11:41.17 3 同步并发 - <NSThread:>{number = 6, name = (null)}
11:42.18 4 同步并发 - <NSThread:>{number = 6, name = (null)}
11:42.18 2 任务结尾 - <NSThread:>{number = 6, name = (null)}

6. 在当前队列开启异步并发队列,两者没有明确的先后顺序(异步任务之前的代码先执行,后边的谁抢到谁执行),新队列任务并发执行,开启多个新线程

dispatch_queue_t queue = dispatch_queue_create("NXqueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(self.bfqueue, ^{

        NSLog(@"1 任务开始 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:1];
        
        dispatch_async(queue, ^{
            [NSThread sleepForTimeInterval:0.5];
            NSLog(@"3 异步并发 - %@",[NSThread currentThread]);
        });
        
        dispatch_async(queue, ^{
            //[NSThread sleepForTimeInterval:0.5];
            NSLog(@"4 异步并发 - %@",[NSThread currentThread]);
        });
        
        //[NSThread sleepForTimeInterval:1];
        NSLog(@"2 任务结尾 - %@",[NSThread currentThread]);
        
    });
打印
234均等待1部分执行完毕
234互相之间无关联,谁先抢到资源谁就调用
14:12.47 1 任务开始 - <NSThread: >{number = 4, name = (null)}
14:13.47 2 任务结尾 - <NSThread: >{number = 4, name = (null)}
14:13.47 4 异步并发 - <NSThread: >{number = 6, name = (null)}
14:13.97 3 异步并发 - <NSThread: >{number = 7, name = (null)}

结束:建议复制代码自己跑跑,干看有点抽象。demo我就不传了,就这些东西,仅供参考,学习。

思考:

  1. 在什么情况下会遇到数据竞争(data race)问题;
  2. 数据竞争怎么解决;
  3. 并发队列如何串行执行;
  4. 什么时候会用到锁。

相关文章

  • iOS GCD简单嵌套测试结论

    前言:最近在做SDK方向,对于线程、锁、高并发等关键词使用很频繁。做这个嵌套测试,主要是搞懂代码的执行顺序,从而对...

  • iOS 多线程

    参考链接 iOS多线程iOS 多线程:『GCD』详尽总结iOS简单优雅的实现复杂情况下的串行需求(各种锁、GCD ...

  • IOS GCD

    转载: IOS GCD GCD 是在iOS开发多线程技术里面,使用最简单,执行效率最高的,是相对底层的API,都是...

  • iOS-多线程篇—GCD介绍

    iOS开发多线程篇—GCD介绍一、简单介绍1.什么是GCD?全称是Grand Central Dispatch,可...

  • 多线程之GCD用法

    iOS开发多线程之GCD介绍 一、简单介绍 1.什么是GCD? 全称是Grand Central Dispatch...

  • 多线程和AFN网络框架配合使用

    ios的多线程一般有NSOperation和GCD.NSOperation基本使用: GCD基本使用: 简单的多线...

  • iOS开发多线程之GCD

    iOS开发多线程之GCDiOS开发之GCD同步任务加强iOS开发之GCD串行队列iOS开发之GCD并发队列 GCD...

  • [iOS 多线程] iOS多线程-GCD

    iOS多线程-GCD GCD的简介 GCD,全称为 Grand Central Dispatch ,是iOS用来管...

  • iOS GCD

    GCD 简介 GCD 任务和队列 GCD 的使用步骤 GCD 的基本使用(六种组合不同区别,队列嵌套情况区别,相互...

  • iOS多线程:『GCD』详尽总结

    iOS多线程:『GCD』详尽总结 iOS多线程:『GCD』详尽总结

网友评论

      本文标题:iOS GCD简单嵌套测试结论

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