GCD 的理解

1. GCD 的两个核心 :任务 和 队列

任务:就是执行操作的意思,换句话说就是你在线程中执行的那段代码。在 GCD 中是放在 block 中的。执行任务有两种方式:同步执行(sync)和异步执行(async)。两者的主要区别是:是否等待队列的任务执行结束,以及是否具备开启新线程的能力。

  • 同步执行(sync):

    • 同步添加任务到指定的队列中,在添加的任务执行结束之前,会一直等待,直到队列里面的任务完成之后再继续执行。
    • 只能在当前线程中执行任务,不具备开启新线程的能力。
  • 异步执行(async):

    • 异步添加任务到指定的队列中,它不会做任何等待,可以继续执行任务。
      可以在新的线程中执行任务,具备开启新线程的能力。

队列(Dispatch Queue):这里的队列指执行任务的等待队列,即用来存放任务的队列。队列是一种特殊的线性表,采用 FIFO(先进先出)的原则,即新任务总是被插入到队列的末尾,而读取任务的时候总是从队列的头部开始读取。每读取一个任务,则从队列中释放一个任务。

  • 串行队列(Serial Dispatch Queue):每次只有一个任务被执行。让任务一个接着一个地执行。只开启一个线程,一个任务执行完毕后,再执行下一个任务
  • 并行队列(Concurrent Dispatch Queue):可以让多个任务并发(同时)执行。可以开启多个线程,并且同时执行任务

个人理解: 队列(Serial和Concurrent) 决定是否 具备 开启线程的能力,sync和async决定是否 开启 另外的线程

开启线程的能力跟所在的线程没关系

截屏2020-11-19下午8.19.36.png

2. 代码解析

代码1
//创建串行队列
    dispatch_queue_t mySerialQueue = dispatch_queue_create("my_test_queue", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t myQueue = dispatch_queue_create("my_test_queue_2", DISPATCH_QUEUE_SERIAL);

    //异步执行+串行队列:开启一个线程
    dispatch_async(mySerialQueue, ^{
        //任务1
        NSLog(@"7-----%@",[NSThread currentThread]);
        NSLog(@"7 === start");
        //异步 不用等待 继续执行
        dispatch_async(myQueue, ^{
             //任务3
            NSLog(@"4 === start");
            for (int i = 0; i<2; i++) {
                [NSThread sleepForTimeInterval:2];
                NSLog(@"4-----%@",[NSThread currentThread]);
            }
            dispatch_async(mySerialQueue, ^{
                 //任务4
                NSLog(@"8 === start");
                for (int i = 0; i<2; i++) {
                    [NSThread sleepForTimeInterval:2];
                    NSLog(@"8-----%@",[NSThread currentThread]);
                }
                NSLog(@"8 === end");
            });
            NSLog(@"4 === end");
        });
        //异步 不用等待继续执行
        dispatch_async(myQueue, ^{
            //任务5
            NSLog(@"5 === start");
            for (int i = 0; i<2; i++) {
                [NSThread sleepForTimeInterval:2];
                NSLog(@"5-----%@",[NSThread currentThread]);
            }
        });
        NSLog(@"7 === end");
    });
    //串行队列 需一个一个执行 :等待 NSLog(@"7 === end") 执行后再执行
    dispatch_async(mySerialQueue, ^{
        //任务2
        NSLog(@"9 === start");
        for (int i = 0; i<2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"9-----%@",[NSThread currentThread]);
        }
        NSLog(@"9 === end");
    });

执行后输出结果是 :

2019-04-16 14:37:09.149808+0800 test123[95964:8582595] 7-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:09.149973+0800 test123[95964:8582595] 7 === start
2019-04-16 14:37:09.150092+0800 test123[95964:8582595] 7 === end
2019-04-16 14:37:09.150102+0800 test123[95964:8582592] 4 === start
2019-04-16 14:37:09.150225+0800 test123[95964:8582595] 9 === start
2019-04-16 14:37:11.153315+0800 test123[95964:8582595] 9-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:11.153338+0800 test123[95964:8582592] 4-----<NSThread: 0x6000020e3040>{number = 4, name = (null)}
2019-04-16 14:37:13.157628+0800 test123[95964:8582595] 9-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:13.157649+0800 test123[95964:8582592] 4-----<NSThread: 0x6000020e3040>{number = 4, name = (null)}
2019-04-16 14:37:13.158006+0800 test123[95964:8582595] 9 === end
2019-04-16 14:37:13.158021+0800 test123[95964:8582592] 4 === end
2019-04-16 14:37:13.158315+0800 test123[95964:8582592] 5 === start
2019-04-16 14:37:13.158366+0800 test123[95964:8582595] 8 === start
2019-04-16 14:37:15.163083+0800 test123[95964:8582592] 5-----<NSThread: 0x6000020e3040>{number = 4, name = (null)}
2019-04-16 14:37:15.163111+0800 test123[95964:8582595] 8-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:17.163967+0800 test123[95964:8582592] 5-----<NSThread: 0x6000020e3040>{number = 4, name = (null)}
2019-04-16 14:37:17.163967+0800 test123[95964:8582595] 8-----<NSThread: 0x6000020ea7c0>{number = 3, name = (null)}
2019-04-16 14:37:17.164365+0800 test123[95964:8582595] 8 === end

分析:任务3(4===start) 比 任务2(9===start) 早输出原因:异步执行 任务3 后,下边代码还有耗时操作(任务5、NSLog)所以 任务2 需要等待,会先异步执行 任务3(4 === start )

代码2
dispatch_group_t group = dispatch_group_create();

    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"1 === start");
        for (int i = 0; i<2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1-----%@",[NSThread currentThread]);
        }
        NSLog(@"1 === end");
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"2 === start");
        for (int i = 0; i<2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2-----%@",[NSThread currentThread]);
        }
        NSLog(@"2 === end");
        dispatch_group_leave(group);
    });
    dispatch_group_enter(group);
    //创建串行队列
    dispatch_queue_t mySerialQueue = dispatch_queue_create("my_test_queue", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t myQueue = dispatch_queue_create("my_test_queue_2", DISPATCH_QUEUE_SERIAL);

    //异步执行+串行队列:开启一个线程
    dispatch_async(mySerialQueue, ^{
        NSLog(@"7-----%@",[NSThread currentThread]);
        NSLog(@"7 === start");
        //异步 不用等待 继续执行
        dispatch_async(myQueue, ^{
            NSLog(@"4 === start");
            for (int i = 0; i<2; i++) {
                [NSThread sleepForTimeInterval:2];
                NSLog(@"4-----%@",[NSThread currentThread]);
            }
            dispatch_async(mySerialQueue, ^{
                NSLog(@"8 === start");
                for (int i = 0; i<2; i++) {
                    [NSThread sleepForTimeInterval:2];
                    NSLog(@"8-----%@",[NSThread currentThread]);
                }
                NSLog(@"8 === end");
            });
            NSLog(@"4 === end");
        });
        //异步 不用等待继续执行
        dispatch_async(myQueue, ^{
            NSLog(@"5 === start");
            for (int i = 0; i<2; i++) {
                [NSThread sleepForTimeInterval:2];
                NSLog(@"5-----%@",[NSThread currentThread]);
            }
        });
        NSLog(@"7 === end");
    });
    //串行队列 需一个一个执行 :等待 NSLog(@"7 === end") 执行后再执行
    dispatch_async(mySerialQueue, ^{
        NSLog(@"9 === start");
        for (int i = 0; i<2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"9-----%@",[NSThread currentThread]);
        }
        NSLog(@"9 === end");
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"3 === start");
        for (int i = 0; i<2; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3-----%@",[NSThread currentThread]);
        }
    });

输出结果:

2019-04-16 14:49:41.670736+0800 test123[96172:8588712] 7-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:41.670807+0800 test123[96172:8588710] 1 === start
2019-04-16 14:49:41.670865+0800 test123[96172:8588711] 2 === start
2019-04-16 14:49:41.670975+0800 test123[96172:8588712] 7 === start
2019-04-16 14:49:41.671074+0800 test123[96172:8588712] 7 === end
2019-04-16 14:49:41.671086+0800 test123[96172:8588709] 4 === start
2019-04-16 14:49:41.671177+0800 test123[96172:8588712] 9 === start
2019-04-16 14:49:43.675862+0800 test123[96172:8588710] 1-----<NSThread: 0x600003597380>{number = 4, name = (null)}
2019-04-16 14:49:43.675871+0800 test123[96172:8588712] 9-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:43.675862+0800 test123[96172:8588711] 2-----<NSThread: 0x60000358af40>{number = 5, name = (null)}
2019-04-16 14:49:43.675864+0800 test123[96172:8588709] 4-----<NSThread: 0x60000358adc0>{number = 6, name = (null)}
2019-04-16 14:49:45.680467+0800 test123[96172:8588711] 2-----<NSThread: 0x60000358af40>{number = 5, name = (null)}
2019-04-16 14:49:45.680547+0800 test123[96172:8588712] 9-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:45.680467+0800 test123[96172:8588710] 1-----<NSThread: 0x600003597380>{number = 4, name = (null)}
2019-04-16 14:49:45.680478+0800 test123[96172:8588709] 4-----<NSThread: 0x60000358adc0>{number = 6, name = (null)}
2019-04-16 14:49:45.680875+0800 test123[96172:8588711] 2 === end
2019-04-16 14:49:45.680874+0800 test123[96172:8588712] 9 === end
2019-04-16 14:49:45.680967+0800 test123[96172:8588709] 4 === end
2019-04-16 14:49:45.680874+0800 test123[96172:8588710] 1 === end
2019-04-16 14:49:45.681135+0800 test123[96172:8588712] 8 === start
2019-04-16 14:49:45.681224+0800 test123[96172:8588709] 5 === start
2019-04-16 14:49:45.681723+0800 test123[96172:8588662] 3 === start
2019-04-16 14:49:47.682394+0800 test123[96172:8588662] 3-----<NSThread: 0x6000035e2940>{number = 1, name = main}
2019-04-16 14:49:47.683973+0800 test123[96172:8588709] 5-----<NSThread: 0x60000358adc0>{number = 6, name = (null)}
2019-04-16 14:49:47.684043+0800 test123[96172:8588712] 8-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:49.683874+0800 test123[96172:8588662] 3-----<NSThread: 0x6000035e2940>{number = 1, name = main}
2019-04-16 14:49:49.684750+0800 test123[96172:8588709] 5-----<NSThread: 0x60000358adc0>{number = 6, name = (null)}
2019-04-16 14:49:49.684799+0800 test123[96172:8588712] 8-----<NSThread: 0x600003583cc0>{number = 3, name = (null)}
2019-04-16 14:49:49.685113+0800 test123[96172:8588712] 8 === end

解析:1和2先后顺序不一定 1、2、4、9相互交替执行,5肯定在4结束后执行,8肯定在9执行完后执行,最后5和8交替执行

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

推荐阅读更多精彩内容