iOS GCD简单嵌套测试结论

前言:最近在做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. 什么时候会用到锁。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容

  • 多线程系列篇章计划内容:iOS多线程编程(一) 多线程基础[https://juejin.im/post/6890...
    卖馍工程师阅读 164评论 0 1
  • apple官方解释 Grand Central Dispatch (GCD) comprises language...
    自律_自强_通达阅读 384评论 0 0
  • 这段时间的研究内容的是锁,因为实际开发中用到的比较少,文中难免会有错误,希望能够多多指正。这篇博客的第一部分是一些...
    kikido阅读 485评论 0 1
  • GCD笔记 总结一下多线程部分,最强大的无疑是GCD,那么先从这一块部分讲起. Dispatch Queue的种类...
    jins_1990阅读 761评论 0 1
  • 目录(GCD): 关键词 混淆点 场景应用 总结 1. 关键词 线程概念: 独立执行的代码段,一个线程同时间只能执...
    Ryan___阅读 1,270评论 0 3