dispatch_group_enter 、 dispatch_group_leave

dispatch_group_enter和dispatch_group_leave必須成對出現(xiàn) ,來給group添加新任務(wù)。

dispatch_group_enter(group)
//執(zhí)行的事件
dispatch_group_leave(group)

為什么會用到這個方法呢?
因為在dispatch_group_async()里使用dispatch_async()方法,dispatch_group_notify不是在最后執(zhí)行的。而dispatch_sync()方法調(diào)用的時候正常。代碼如下:

一、使用dispatch_group_async()方法
1、異步里異步并發(fā)

    dispatch_queue_t queue = dispatch_queue_create("dispatchGroupMethod1.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, queue, ^{
        dispatch_async(queue, ^{
            for (NSInteger i =0; i<3; i++) {
                sleep(1);
                NSLog(@"任務(wù)1-異步任務(wù)執(zhí)行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
            }
        });
    });
    
    dispatch_group_async(group, queue, ^{
        dispatch_async(queue, ^{
            for (NSInteger i =0; i<3; i++) {
                sleep(1);
                NSLog(@"任務(wù)2-異步任務(wù)執(zhí)行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
            }
        });
    });
    
    //等待上面的任務(wù)全部完成后,會收到通知執(zhí)行block中的代碼 (不會阻塞線程)
    dispatch_group_notify(group, queue, ^{
        NSLog(@"全部任務(wù)執(zhí)行完成");
    });
//打印如下
2018-03-09 16:54:37.009913+0800 ceshi[3934:349413] 全部任務(wù)執(zhí)行完成
2018-03-09 16:54:38.014204+0800 ceshi[3934:349411] 任務(wù)1-異步任務(wù)執(zhí)行-:0,thread:<NSThread: 0x60000026cb00>{number = 3, name = (null)}
2018-03-09 16:54:38.014204+0800 ceshi[3934:349414] 任務(wù)2-異步任務(wù)執(zhí)行-:0,thread:<NSThread: 0x6040004799c0>{number = 4, name = (null)}
2018-03-09 16:54:39.018626+0800 ceshi[3934:349414] 任務(wù)2-異步任務(wù)執(zhí)行-:1,thread:<NSThread: 0x6040004799c0>{number = 4, name = (null)}
2018-03-09 16:54:39.018626+0800 ceshi[3934:349411] 任務(wù)1-異步任務(wù)執(zhí)行-:1,thread:<NSThread: 0x60000026cb00>{number = 3, name = (null)}
2018-03-09 16:54:40.020653+0800 ceshi[3934:349411] 任務(wù)1-異步任務(wù)執(zhí)行-:2,thread:<NSThread: 0x60000026cb00>{number = 3, name = (null)}
2018-03-09 16:54:40.020653+0800 ceshi[3934:349414] 任務(wù)2-異步任務(wù)執(zhí)行-:2,thread:<NSThread: 0x6040004799c0>{number = 4, name = (null)}

異步并發(fā),dispatch_group_notify先執(zhí)行,沒在最后調(diào)用。

2、異步里同步并發(fā):

    dispatch_queue_t queue = dispatch_queue_create("dispatchGroupMethod1.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        dispatch_sync(queue, ^{
            for (NSInteger i =0; i<3; i++) {
                sleep(1);
                NSLog(@"任務(wù)1-同步任務(wù)執(zhí)行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
            }
        });
    });
    
    dispatch_group_async(group, queue, ^{
        dispatch_sync(queue, ^{
            for (NSInteger i =0; i<3; i++) {
                sleep(1);
                NSLog(@"任務(wù)2-同步任務(wù)執(zhí)行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
            }
        });
    });
    
    //等待上面的任務(wù)全部完成后,會收到通知執(zhí)行block中的代碼 (不會阻塞線程)
    dispatch_group_notify(group, queue, ^{
        NSLog(@"全部任務(wù)執(zhí)行完成");
    });
//打印如下
2018-03-09 16:56:42.372297+0800 ceshi[3985:353717] 任務(wù)2-同步任務(wù)執(zhí)行-:0,thread:<NSThread: 0x604000273e40>{number = 4, name = (null)}
2018-03-09 16:56:42.372297+0800 ceshi[3985:353715] 任務(wù)1-同步任務(wù)執(zhí)行-:0,thread:<NSThread: 0x604000273e00>{number = 3, name = (null)}
2018-03-09 16:56:43.374677+0800 ceshi[3985:353717] 任務(wù)2-同步任務(wù)執(zhí)行-:1,thread:<NSThread: 0x604000273e40>{number = 4, name = (null)}
2018-03-09 16:56:43.374677+0800 ceshi[3985:353715] 任務(wù)1-同步任務(wù)執(zhí)行-:1,thread:<NSThread: 0x604000273e00>{number = 3, name = (null)}
2018-03-09 16:56:44.380101+0800 ceshi[3985:353715] 任務(wù)1-同步任務(wù)執(zhí)行-:2,thread:<NSThread: 0x604000273e00>{number = 3, name = (null)}
2018-03-09 16:56:44.380101+0800 ceshi[3985:353717] 任務(wù)2-同步任務(wù)執(zhí)行-:2,thread:<NSThread: 0x604000273e40>{number = 4, name = (null)}
2018-03-09 16:56:44.380617+0800 ceshi[3985:353717] 全部任務(wù)執(zhí)行完成

二、使用dispatch_group_enter和dispatch_group_leave

1、異步執(zhí)行

//異步
dispatch_queue_t queue = dispatch_queue_create("dispatchGroupMethod1.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_async(queue, ^{
        dispatch_group_enter(group);
        for (NSInteger i =0; i<3; i++) {
            sleep(1);
            NSLog(@"任務(wù)1-異步任務(wù)執(zhí)行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
        }
        dispatch_group_leave(group);
    });
    
    dispatch_async(queue, ^{
        dispatch_group_enter(group);
        for (NSInteger i =0; i<3; i++) {
            sleep(1);
            NSLog(@"任務(wù)2-異步任務(wù)執(zhí)行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
        }
        dispatch_group_leave(group);
    });
    //等待上面的任務(wù)全部完成后,會收到通知執(zhí)行block中的代碼 (不會阻塞線程)
    dispatch_group_notify(group, queue, ^{
        NSLog(@"全部任務(wù)執(zhí)行完成");
    });
//結(jié)果
2018-03-09 17:28:47.644739+0800 ceshi[4322:392667] 任務(wù)2-異步任務(wù)執(zhí)行-:0,thread:<NSThread: 0x600000266b00>{number = 4, name = (null)}
2018-03-09 17:28:47.644739+0800 ceshi[4322:392666] 任務(wù)1-異步任務(wù)執(zhí)行-:0,thread:<NSThread: 0x60400027dbc0>{number = 3, name = (null)}
2018-03-09 17:28:48.646245+0800 ceshi[4322:392667] 任務(wù)2-異步任務(wù)執(zhí)行-:1,thread:<NSThread: 0x600000266b00>{number = 4, name = (null)}
2018-03-09 17:28:48.646245+0800 ceshi[4322:392666] 任務(wù)1-異步任務(wù)執(zhí)行-:1,thread:<NSThread: 0x60400027dbc0>{number = 3, name = (null)}
2018-03-09 17:28:49.650996+0800 ceshi[4322:392667] 任務(wù)2-異步任務(wù)執(zhí)行-:2,thread:<NSThread: 0x600000266b00>{number = 4, name = (null)}
2018-03-09 17:28:49.650996+0800 ceshi[4322:392666] 任務(wù)1-異步任務(wù)執(zhí)行-:2,thread:<NSThread: 0x60400027dbc0>{number = 3, name = (null)}
2018-03-09 17:28:49.651347+0800 ceshi[4322:392666] 全部任務(wù)執(zhí)行完成

2、同步執(zhí)行

//同步
 dispatch_queue_t queue = dispatch_queue_create("dispatchGroupMethod1.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_sync(queue, ^{
        dispatch_group_enter(group);
        for (NSInteger i =0; i<3; i++) {
            sleep(1);
            NSLog(@"任務(wù)1-同步任務(wù)執(zhí)行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
        }
        dispatch_group_leave(group);
    });
    
    dispatch_sync(queue, ^{
        dispatch_group_enter(group);
        for (NSInteger i =0; i<3; i++) {
            sleep(1);
            NSLog(@"任務(wù)2-同步任務(wù)執(zhí)行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
        }
        dispatch_group_leave(group);
    });
    //等待上面的任務(wù)全部完成后,會收到通知執(zhí)行block中的代碼 (不會阻塞線程)
    dispatch_group_notify(group, queue, ^{
        NSLog(@"全部任務(wù)執(zhí)行完成");
    });

//結(jié)果
2018-03-09 17:42:34.271283+0800 ceshi[4454:413740] 任務(wù)1-同步任務(wù)執(zhí)行-:0,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:35.272241+0800 ceshi[4454:413740] 任務(wù)1-同步任務(wù)執(zhí)行-:1,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:36.273663+0800 ceshi[4454:413740] 任務(wù)1-同步任務(wù)執(zhí)行-:2,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:37.275635+0800 ceshi[4454:413740] 任務(wù)2-同步任務(wù)執(zhí)行-:0,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:38.276095+0800 ceshi[4454:413740] 任務(wù)2-同步任務(wù)執(zhí)行-:1,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:39.277732+0800 ceshi[4454:413740] 任務(wù)2-同步任務(wù)執(zhí)行-:2,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:39.278429+0800 ceshi[4454:413852] 全部任務(wù)執(zhí)行完成

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文首發(fā)于我的個人博客:「程序員充電站」[https://itcharge.cn]文章鏈接:「傳送門」[https...
    ITCharge閱讀 350,711評論 308 1,927
  • 背景 擔(dān)心了兩周的我終于輪到去醫(yī)院做胃鏡檢查了!去的時候我都想好了最壞的可能(胃癌),之前在網(wǎng)上查的癥狀都很相似。...
    Dely閱讀 9,402評論 21 42
  • 同步/異步 同步:多個任務(wù)情況下,一個任務(wù)A執(zhí)行結(jié)束,才可以執(zhí)行另一個任務(wù)B。只存在一個線程也就是主線程。 異步:...
    XLsn0w閱讀 336評論 0 0
  • <head> 或 <body> 中的 JavaScript 您可以在 HTML 文檔中放入不限數(shù)量的腳本。腳本可位...
    海娩閱讀 207評論 0 0
  • 一場看不見的雨 撐開了不屬于我的傘 是游蕩在風(fēng)中的行人
    Cheer_up閱讀 148評論 0 0

友情鏈接更多精彩內(nèi)容