GCD進(jìn)階

GCD 控制線程數(shù)量

GCD 不像NSOperation 那樣有直接提供線程數(shù)量控制方法劫拗,但是通過GCD的semaphore 功能一樣可以達(dá)到控制線程數(shù)量的效果实束。

  1. dispatch_semaphore_create(long value);利用給定的輸出時創(chuàng)建一個新的可計(jì)數(shù)的信號量
  2. dispatch_semaphore_wait(dispatch_semaphore_t dsema,dispatch_time_t timeout);如果信號量大于0,信號量減1哟玷,執(zhí)行程序纵穿。否則等待信號量
  3. dispatch_semaphore_signal(dispatch_semaphore_t dsema);增加信號量

信號量其實(shí)就是用來保證訪問資源的線程數(shù)蓖柔,當(dāng)信號量大于等于1時,資源可以訪問闸昨,否則無法訪問資源蚯斯,直到其它線程釋放資源。

這里主要有三個函數(shù):

long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);   //信號量-1
long dispatch_semaphore_signal(dispatch_semaphore_t dsema);   //信號量+1
// 控制線程數(shù)量
- (void)runMaxThreadCountWithGCD
{
    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentRunMaxThreadCountWithGCD", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t serialQueue = dispatch_queue_create("serialRunMaxThreadCountWithGCD", DISPATCH_QUEUE_SERIAL);
    // 創(chuàng)建一個semaphore,并設(shè)置最大信號量饵较,最大信號量表示最大線程數(shù)量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
    // 使用循環(huán)往串行隊(duì)列 serialQueue 增加 10 個任務(wù)
    for (int i = 0; i < 10 ; i++) {
        dispatch_async(serialQueue, ^{
            // 只有當(dāng)信號量大于 0 的時候溉跃,線程將信號量減 1,程序向下執(zhí)行
            // 否則線程會阻塞并且一直等待告抄,直到信號量大于 0
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_async(concurrentQueue, ^{
                NSLog(@"%@ 執(zhí)行任務(wù)一次  i = %d",[NSThread currentThread],i);
                // 當(dāng)線程任務(wù)執(zhí)行完成之后撰茎,發(fā)送一個信號,增加信號量打洼。
                dispatch_semaphore_signal(semaphore);
            });
        });
    }
    NSLog(@"%@ 執(zhí)行任務(wù)結(jié)束",[NSThread currentThread]);
}

執(zhí)行結(jié)果如下龄糊,只有 number 3 和 number 4 這 2 個線程在執(zhí)行

<nsthread: 0x60c00007c600>{number = 1, name = main} 執(zhí)行任務(wù)結(jié)束
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 執(zhí)行任務(wù)一次  i = 0
<nsthread: 0x608000263a00>{number = 4, name = (null)} 執(zhí)行任務(wù)一次  i = 1
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 執(zhí)行任務(wù)一次  i = 3
<nsthread: 0x608000263a00>{number = 4, name = (null)} 執(zhí)行任務(wù)一次  i = 2
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 執(zhí)行任務(wù)一次  i = 4
<nsthread: 0x608000263a00>{number = 4, name = (null)} 執(zhí)行任務(wù)一次  i = 5
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 執(zhí)行任務(wù)一次  i = 6
<nsthread: 0x608000263a00>{number = 4, name = (null)} 執(zhí)行任務(wù)一次  i = 7
<nsthread: 0x60c00027a340>{number = 3, name = (null)} 執(zhí)行任務(wù)一次  i = 8
<nsthread: 0x608000263a00>{number = 4, name = (null)} 執(zhí)行任務(wù)一次  i = 9</nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x608000263a00></nsthread: 0x60c00027a340></nsthread: 0x60c00007c600>

GCD 任務(wù)分組

GCD 的 dispatch_group_t 功能可以將多個任務(wù)分組,等待分組里面的所有任務(wù)執(zhí)行完成之后募疮,GCD 的 dispatch_group_notify 方法可以通知炫惩。通常會配合一些常見的場景來考察,比如同時上傳 10 張圖片阿浓,全部上傳完成后通知用戶他嚷。

// 任務(wù)分組
- (void)runGroupWithGCD
{
    dispatch_queue_t concurrentQueue = dispatch_queue_create("runGroupWithGCD", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group =  dispatch_group_create();
    for (int i = 0; i < 10 ; i++) {
        dispatch_group_async(group, concurrentQueue, ^{
            NSLog(@"%@ 執(zhí)行任務(wù)一次",[NSThread currentThread]);
        });
    }
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"%@ 執(zhí)行任務(wù)結(jié)束",[NSThread currentThread]);
    });
}

將所有的任務(wù)都加入 group ,等待所有的任務(wù)執(zhí)行完成后,dispatch_group_notify 會被調(diào)用筋蓖。

<nsthread: 0x608000265180>{number = 4, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x604000079a40>{number = 6, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x60c000268780>{number = 5, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x60c000267dc0>{number = 3, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x608000265080>{number = 9, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x600000265480>{number = 7, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x60c00007f9c0>{number = 8, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x608000264f40>{number = 10, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x604000079a40>{number = 6, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x608000265180>{number = 4, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x60000006d180>{number = 1, name = main} 執(zhí)行任務(wù)結(jié)束</nsthread: 0x60000006d180></nsthread: 0x608000265180></nsthread: 0x604000079a40></nsthread: 0x608000264f40></nsthread: 0x60c00007f9c0></nsthread: 0x600000265480></nsthread: 0x608000265080></nsthread: 0x60c000267dc0></nsthread: 0x60c000268780></nsthread: 0x604000079a40></nsthread: 0x608000265180>

GCD 任務(wù)分組和線程數(shù)量控制

利用 GCD 的 dispatch_group_t 和 semaphore 功能卸耘,我們可以做到控制線程數(shù)量,并且在所有任務(wù)執(zhí)行完成之后得到通知粘咖。

// 任務(wù)分組 + 線程數(shù)量控制
- (void)runMaxCountInGroupWithGCD
{
    dispatch_queue_t concurrentQueue = dispatch_queue_create("runGroupWithGCD", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group =  dispatch_group_create();
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
    for (int i = 0; i < 10 ; i++) {
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_group_async(group, concurrentQueue, ^{
            NSLog(@"%@ 執(zhí)行任務(wù)一次",[NSThread currentThread]);
            dispatch_semaphore_signal(semaphore);
        });
    }
     
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"%@ 執(zhí)行任務(wù)結(jié)束",[NSThread currentThread]);
    });
}

執(zhí)行之后蚣抗,我們可以看到既控制了線程數(shù)量,也在執(zhí)行任務(wù)完成之后得到了通知瓮下。

<nsthread: 0x604000269b40>{number = 3, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x604000269b40>{number = 3, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x604000269b40>{number = 3, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x608000264780>{number = 4, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x604000269b40>{number = 3, name = (null)} 執(zhí)行任務(wù)一次
<nsthread: 0x60400007aa40>{number = 1, name = main} 執(zhí)行任務(wù)結(jié)束</nsthread: 0x60400007aa40></nsthread: 0x604000269b40></nsthread: 0x608000264780></nsthread: 0x604000269b40></nsthread: 0x608000264780></nsthread: 0x608000264780></nsthread: 0x608000264780></nsthread: 0x604000269b40></nsthread: 0x608000264780></nsthread: 0x604000269b40>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末翰铡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子讽坏,更是在濱河造成了極大的恐慌锭魔,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件路呜,死亡現(xiàn)場離奇詭異赂毯,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)拣宰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門党涕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人巡社,你說我怎么就攤上這事膛堤。” “怎么了晌该?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵肥荔,是天一觀的道長。 經(jīng)常有香客問我朝群,道長燕耿,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任姜胖,我火速辦了婚禮誉帅,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘右莱。我一直安慰自己蚜锨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布慢蜓。 她就那樣靜靜地躺著亚再,像睡著了一般。 火紅的嫁衣襯著肌膚如雪晨抡。 梳的紋絲不亂的頭發(fā)上氛悬,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天则剃,我揣著相機(jī)與錄音,去河邊找鬼如捅。 笑死棍现,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的伪朽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼汛蝙,長吁一口氣:“原來是場噩夢啊……” “哼烈涮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起窖剑,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤坚洽,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后西土,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體讶舰,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年需了,在試婚紗的時候發(fā)現(xiàn)自己被綠了跳昼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡肋乍,死狀恐怖鹅颊,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情墓造,我是刑警寧澤堪伍,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站觅闽,受9級特大地震影響帝雇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蛉拙,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一尸闸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧孕锄,春花似錦室叉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至恼除,卻和暖如春踪旷,著一層夾襖步出監(jiān)牢的瞬間曼氛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工令野, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留舀患,地道東北人。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓气破,卻偏偏與公主長得像聊浅,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子现使,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容