并行 | 串行 | |
---|---|---|
創(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ù):
- 蒸米飯
淘米、漫長(zhǎng)的蒸米過程 - 做菜
擇菜聂示、洗菜域携、切菜、切肉鱼喉、腌肉秀鞭、炒菜、悶熱20秒 - 做完了叫人來吃飯
好了扛禽,假設(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#>)