GCD

并行 串行
創(chuàng)建方法 dispatch_queue_create("com.yunding.locklock.ble", DISPATCH_QUEUE_SERIAL); dispatch_queue_create("com.yunding.locklock.ble", DISPATCH_QUEUE_SERIAL);
系統(tǒng)默認(rèn) dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_get_main_queue()
dispatch_queue_t
dispatch_queue_create(const char *_Nullable label,
        dispatch_queue_attr_t _Nullable attr);

/*!
 * @typedef dispatch_queue_attr_t
 *
 * @abstract
 * Attribute for dispatch queues.
 */
DISPATCH_DECL(dispatch_queue_attr);

/*!
 * @const DISPATCH_QUEUE_SERIAL
 *
 * @discussion A dispatch queue that invokes blocks serially in FIFO order.
 */
#define DISPATCH_QUEUE_SERIAL NULL

/*!
 * @const DISPATCH_QUEUE_SERIAL_INACTIVE
 *
 * @discussion
 * A dispatch queue that invokes blocks serially in FIFO order, and that is
 * created initially inactive. See dispatch_queue_attr_make_initially_inactive().
 */
#define DISPATCH_QUEUE_SERIAL_INACTIVE \
        dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_SERIAL)

/*!
 * @const DISPATCH_QUEUE_CONCURRENT
 *
 * @discussion A dispatch queue that may invoke blocks concurrently and supports
 * barrier blocks submitted with the dispatch barrier API.
 */
#define DISPATCH_QUEUE_CONCURRENT \
        DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, \
        _dispatch_queue_attr_concurrent)
__OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
DISPATCH_EXPORT
struct dispatch_queue_attr_s _dispatch_queue_attr_concurrent;

/*!
 * @const DISPATCH_QUEUE_CONCURRENT_INACTIVE
 *
 * @discussion
 * A dispatch queue that may invoke blocks concurrently and supports barrier
 * blocks submitted with the dispatch barrier API, and that is created initially
 * inactive. See dispatch_queue_attr_make_initially_inactive().
 */
#define DISPATCH_QUEUE_CONCURRENT_INACTIVE \
        dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_CONCURRENT)


dispatch_queue_attr_t 宏定義
串行 DISPATCH_QUEUE_SERIAL NULL
DISPATCH_QUEUE_SERIAL_INACTIVE dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_SERIAL)
DISPATCH_QUEUE_CONCURRENT DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, _dispatch_queue_attr_concurrent)
DISPATCH_QUEUE_CONCURRENT_INACTIVE dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_CONCURRENT)
dispatch_queue_t
dispatch_get_global_queue(long identifier, unsigned long flags);
 * well-known global concurrent queues:
 *  - QOS_CLASS_USER_INTERACTIVE
 *  - QOS_CLASS_USER_INITIATED
 *  - QOS_CLASS_DEFAULT
 *  - QOS_CLASS_UTILITY
 *  - QOS_CLASS_BACKGROUND
 *
 * The global concurrent queues may still be identified by their priority,
 * which map to the following QOS classes:
 *  - DISPATCH_QUEUE_PRIORITY_HIGH:         QOS_CLASS_USER_INITIATED
 *  - DISPATCH_QUEUE_PRIORITY_DEFAULT:      QOS_CLASS_DEFAULT
 *  - DISPATCH_QUEUE_PRIORITY_LOW:          QOS_CLASS_UTILITY
 *  - DISPATCH_QUEUE_PRIORITY_BACKGROUND:   QOS_CLASS_BACKGROUND
 *
 * @param flags
 * Reserved for future use. Passing any value other than zero may result in
 * a NULL return value.

dispatch_queue_priority_t 宏定義
DISPATCH_QUEUE_PRIORITY_HIGH 2 * Items dispatched to the queue will run at high priority, the queue will be scheduled for execution before any default priority or low priority queue.
DISPATCH_QUEUE_PRIORITY_DEFAULT 0 * Items dispatched to the queue will run at the default priority, i.e. the queue will be scheduled for execution after all high priority queues have been scheduled, but before any low priority queues have been scheduled.
DISPATCH_QUEUE_PRIORITY_LOW -2
DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN * Items dispatched to the queue will run at background priority, i.e. the queue will be scheduled for execution after all higher priority queues have been scheduled and the system will run items on this queue on a thread with background status as per setpriority(2) (i.e. disk I/O is throttled and the thread's scheduling priority is set to lowest value).

舉個(gè)做飯的例子:
我們要做一頓飯职辅,這個(gè)任務(wù)可以細(xì)分為下面幾個(gè)任務(wù):

  1. 蒸米飯
    淘米、漫長(zhǎng)的蒸米過程
  2. 做菜
    擇菜聂示、洗菜域携、切菜、切肉鱼喉、腌肉秀鞭、炒菜、悶熱20秒
  3. 做完了叫人來吃飯

好了扛禽,假設(shè)有好多小伙伴锋边,可以一起來做來提高效率。
米飯和菜可以同時(shí)來分開做编曼,
A擇菜豆巨,B切肉,C淘米
其中擇菜掐场、洗菜往扔、切菜是有先后順序的,
切肉熊户、腌肉也有順序萍膛,淘米、蒸米也有順序
炒菜必須等菜和肉都到位才能進(jìn)行

dispatch_queue_t queue1 = dispatch_queue_create("米", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue2 = dispatch_queue_create("菜", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue3 = dispatch_queue_create("肉", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue4 = dispatch_queue_create("炒菜", DISPATCH_QUEUE_SERIAL);

    dispatch_async(queue1, ^{
        //淘米
    });
    dispatch_async(queue1, ^{
        //蒸米
    });
    
    dispatch_async(queue2, ^{
        //擇菜敏弃,有好多菜卦羡,可以分頭擇噪馏,并行
    });
     dispatch_barrier_async(queue2, ^{
         //洗菜麦到,利用柵欄塊來完成順序執(zhí)行
     });
    dispatch_barrier_async(queue2, ^{
        //切菜,利用柵欄塊來完成順序執(zhí)行
    });
    
    dispatch_async(queue3, ^{
        //洗肉
    });
    dispatch_async(queue3, ^{
        //腌肉
    });
    dispatch_async(queue3, ^{
        //切肉
    });
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue2, ^{
       //擇菜欠肾,有好多菜瓶颠,可以分頭擇,并行
    });
    dispatch_group_async(group, queue2, ^{
        //洗菜刺桃,利用柵欄塊來完成順序執(zhí)行
    });
    dispatch_group_async(group, queue2, ^{
        //切菜粹淋,利用柵欄塊來完成順序執(zhí)行
    });
    
    dispatch_group_async(group, queue3, ^{
        //洗肉
    });
    dispatch_group_async(group, queue3, ^{
        //腌肉
    });
    dispatch_group_async(group, queue3, ^{
        //切肉
    });
    
    dispatch_group_wait(group, 20*60);//同步等待完成
    dispatch_group_notify(group, queue4, ^{
        //材料準(zhǔn)備完畢,炒菜
        
    });
    

此外:
dispatch_barrier_async的順序執(zhí)行還是依賴queue的類型啊,必需要queue的類型為dispatch_queue_create創(chuàng)建的桃移,而且attr參數(shù)值必需是DISPATCH_QUEUE_CONCURRENT類型屋匕,dispatch_get_global_queue獲得的并發(fā)隊(duì)列被歧視了。
延時(shí)執(zhí)行dispatch_after
執(zhí)行n次(通過數(shù)組的形式封裝任務(wù)借杰,以實(shí)現(xiàn)group的效果)dispatch_apply(<#size_t iterations#>, <#dispatch_queue_t _Nonnull queue#>, <#^(size_t)block#>)
執(zhí)行1次 dispatch_once(<#dispatch_once_t * _Nonnull predicate#>, <#^(void)block#>)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末过吻,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蔗衡,更是在濱河造成了極大的恐慌纤虽,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,639評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绞惦,死亡現(xiàn)場(chǎng)離奇詭異逼纸,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)济蝉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,277評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門杰刽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人王滤,你說我怎么就攤上這事专缠。” “怎么了淑仆?”我有些...
    開封第一講書人閱讀 157,221評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵涝婉,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我蔗怠,道長(zhǎng)墩弯,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,474評(píng)論 1 283
  • 正文 為了忘掉前任寞射,我火速辦了婚禮渔工,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘桥温。我一直安慰自己引矩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,570評(píng)論 6 386
  • 文/花漫 我一把揭開白布侵浸。 她就那樣靜靜地躺著旺韭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掏觉。 梳的紋絲不亂的頭發(fā)上区端,一...
    開封第一講書人閱讀 49,816評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音澳腹,去河邊找鬼织盼。 笑死杨何,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的沥邻。 我是一名探鬼主播危虱,決...
    沈念sama閱讀 38,957評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼唐全!你這毒婦竟也來了槽地?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,718評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤芦瘾,失蹤者是張志新(化名)和其女友劉穎捌蚊,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體近弟,經(jīng)...
    沈念sama閱讀 44,176評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡缅糟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,511評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了祷愉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窗宦。...
    茶點(diǎn)故事閱讀 38,646評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖二鳄,靈堂內(nèi)的尸體忽然破棺而出赴涵,到底是詐尸還是另有隱情,我是刑警寧澤订讼,帶...
    沈念sama閱讀 34,322評(píng)論 4 330
  • 正文 年R本政府宣布髓窜,位于F島的核電站,受9級(jí)特大地震影響欺殿,放射性物質(zhì)發(fā)生泄漏寄纵。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,934評(píng)論 3 313
  • 文/蒙蒙 一脖苏、第九天 我趴在偏房一處隱蔽的房頂上張望程拭。 院中可真熱鬧,春花似錦棍潘、人聲如沸恃鞋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,755評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恤浪。三九已至,卻和暖如春鳍徽,著一層夾襖步出監(jiān)牢的瞬間资锰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,987評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工阶祭, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绷杜,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,358評(píng)論 2 360
  • 正文 我出身青樓濒募,卻偏偏與公主長(zhǎng)得像鞭盟,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子瑰剃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,514評(píng)論 2 348

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