CGD基本介紹(三)

GCD:Grand Central Dispatch(牛逼的中樞調度器)

GCD兩個重要的概念:任務猩谊、隊列

創(chuàng)建CGD:

  • 定制任務
  • 將任務添加到隊列中

隊列:

  • 并發(fā)(異步)隊列(Concurrent dispatch queue):可以讓多個任務并發(fā)(同時)執(zhí)行讹堤,自動開啟多個線程同時執(zhí)行任務。并發(fā)只在異步函數(shù)下有效
  • 串行隊列(Serial dispatch queue):讓任務一個接著一個的執(zhí)行(一個任務執(zhí)行完才能執(zhí)行下個任務)

GCD執(zhí)行任務的常用函數(shù):

  • 同步:只能在當前線程中執(zhí)行任務宝泵,不具備開啟線程的能力(同步函數(shù)會立馬執(zhí)行)
  • 異步:可以在新的線程中執(zhí)行任務好啰,具備開啟新線程的能力

代碼中獲取并發(fā)隊列:

// 創(chuàng)建并發(fā)隊列
dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_CONCURRENT);
// 獲取全局的并發(fā)隊列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

代碼中獲取串行隊列:

// 創(chuàng)建串行隊列
dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_SERIAL);
// 獲取主隊列,主隊列是GCD自帶的一種特殊的串行隊列儿奶,主隊列中的任務都會放到主線程中運行

/**
 * 異步函數(shù) + 并發(fā)隊列:可以同時開啟多條線程
 */
- (void)asyncConcurrent {
    NSLog(@"asyncConcurrent--------start");
    /* 1.創(chuàng)建一個并發(fā)隊列
     * label : 相當于隊列的名字框往,唯一的標示
     * DISPATCH_QUEUE_CONCURRENT  并發(fā)隊列;DISPATCH_QUEUE_SERIAL  串行隊列
     */
    // dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_CONCURRENT);
    
    /* 1.獲得全局的并發(fā)隊列(GCD默認提供了全局的并發(fā)隊列闯捎,供整個程序使用椰弊,不需創(chuàng)建)
     * dispatch_get_global_queue(long identifier, unsigned long flags)
     * long identifier:隊列優(yōu)先級,下面列出的優(yōu)先級從高到低
     * DISPATCH_QUEUE_PRIORITY_HIGH
     * DISPATCH_QUEUE_PRIORITY_DEFAULT  (默認)
     * DISPATCH_QUEUE_PRIORITY_LOW
     * DISPATCH_QUEUE_PRIORITY_BACKGROUND
     * flags默認傳0
     */
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 2.將任務加入隊列
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    NSLog(@"asyncConcurrent--------end");
}

結果:


image
/**
 * 同步函數(shù) + 并發(fā)隊列:不會開啟新的線程
 */
- (void)syncConcurrent {
    NSLog(@"syncConcurrent--------start");
    // 1.獲得全局的并發(fā)隊列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 2.將任務加入隊列
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"syncConcurrent--------end");
}

結果:


image
/**
 * 異步函數(shù) + 串行隊列:會開啟新的線程瓤鼻,但是任務是串行的秉版,執(zhí)行完一個任務,再執(zhí)行下一個任務
 */
- (void)asyncSerial {
    NSLog(@"asyncSerial--------start");
    // 1.創(chuàng)建串行隊列
    dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_SERIAL);
//    dispatch_queue_t queue = dispatch_queue_create("com.520it.queue", NULL);
    
    // 2.將任務加入隊列
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    NSLog(@"asyncSerial--------end");
}

結果:


image
/**
 * 同步函數(shù) + 串行隊列:不會開啟新的線程娱仔,在當前線程執(zhí)行任務沐飘。任務是串行的,執(zhí)行完一個任務牲迫,再執(zhí)行下一個任務
 */
- (void)syncSerial {
    NSLog(@"syncSerial--------start");
    // 1.創(chuàng)建串行隊列
    dispatch_queue_t queue = dispatch_queue_create("com.weizuche.queue", DISPATCH_QUEUE_SERIAL);
    
    // 2.將任務加入隊列
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    NSLog(@"syncSerial--------end");
}

結果:


image
/**
 * 異步函數(shù) + 主隊列:只在主線程中執(zhí)行任務耐朴,異步函數(shù)在主隊列上不會開啟線程
 */
- (void)asyncMain
{
    NSLog(@"asyncMain ----- start");
    // 1.獲得主隊列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 2.將任務加入隊列
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"1-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"2-----%@", [NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<5; i++) {
            NSLog(@"3-----%@", [NSThread currentThread]);
        }
    });
    NSLog(@"asyncMain ----- end");
}

結果:


image
/**
 * 同步函數(shù) + 主隊列:
 */
- (void)syncMain
{
    NSLog(@"syncMain ----- begin");
    
    // 1.獲得主隊列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 2.將任務加入隊列
    dispatch_sync(queue, ^{
        NSLog(@"1-----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"2-----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"3-----%@", [NSThread currentThread]);
    });
    
    NSLog(@"syncMain ----- end");
}

結果:


image

出現(xiàn)這種情況的原因:主線程先執(zhí)行syncMain函數(shù),但是在syncMain函數(shù)中走到dispatch_sync(queue, ^{
NSLog(@"1-----%@", [NSThread currentThread]);
});的時候盹憎,因為這句話是同步主隊列中執(zhí)行筛峭,需要優(yōu)先執(zhí)行這句話,但是syncMain還沒執(zhí)行完陪每,同樣需要先執(zhí)行完syncMain函數(shù)影晓,造成沖突镰吵,所以就出現(xiàn)了上圖的情況。

線程之間的通信
dispatch_queue_t queue = dispatch_queue_create("11111",  DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        // 進行耗時的操作呢
        dispatch_async(dispatch_get_main_queue(), ^{
            // 回到主線程進行UI刷新
    });
});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末挂签,一起剝皮案震驚了整個濱河市疤祭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌饵婆,老刑警劉巖勺馆,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異侨核,居然都是意外死亡草穆,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門搓译,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悲柱,“玉大人,你說我怎么就攤上這事些己⊥慵Γ” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵轴总,是天一觀的道長直颅。 經常有香客問我博个,道長怀樟,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任盆佣,我火速辦了婚禮往堡,結果婚禮上,老公的妹妹穿的比我還像新娘共耍。我一直安慰自己虑灰,他們只是感情好,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布痹兜。 她就那樣靜靜地躺著穆咐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪字旭。 梳的紋絲不亂的頭發(fā)上对湃,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機與錄音遗淳,去河邊找鬼拍柒。 笑死,一個胖子當著我的面吹牛屈暗,可吹牛的內容都是我干的拆讯。 我是一名探鬼主播脂男,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼种呐!你這毒婦竟也來了宰翅?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤爽室,失蹤者是張志新(化名)和其女友劉穎堕油,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肮之,經...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡掉缺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了戈擒。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片眶明。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖筐高,靈堂內的尸體忽然破棺而出搜囱,到底是詐尸還是另有隱情,我是刑警寧澤柑土,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布蜀肘,位于F島的核電站,受9級特大地震影響稽屏,放射性物質發(fā)生泄漏扮宠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一狐榔、第九天 我趴在偏房一處隱蔽的房頂上張望坛增。 院中可真熱鬧,春花似錦薄腻、人聲如沸收捣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽罢艾。三九已至,卻和暖如春尽纽,著一層夾襖步出監(jiān)牢的瞬間咐蚯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工蜓斧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留仓蛆,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓挎春,卻偏偏與公主長得像看疙,于是被迫代替她去往敵國和親豆拨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355