1. 自己創建隊列
構造函數
/*
- label : 隊列的名稱 - attributes : 隊列的屬性
* public static let serial: DispatchQueueAttributes // 串行
* public static let concurrent: DispatchQueueAttributes // 并發
* public static let initiallyInactive: DispatchQueueAttributes
* public static let autoreleaseInherit: DispatchQueueAttributes
* public static let autoreleaseWorkItem: DispatchQueueAttributes
* public static let autoreleaseNever: DispatchQueueAttributes
* public static let qosUserInteractive: DispatchQueueAttributes // 用戶交互(希望盡快完成,用戶很希望得到結果。個人覺得這個和主線的的線程優先級是一樣的)
* public static let qosUserInitiated: DispatchQueueAttributes // 用戶期望優先級(不要放太耗時的操作)
* public static let qosDefault: DispatchQueueAttributes // 默認優先級(一般不是給程序員實用的,用來重置隊列用的)
* public static let qosUtility: DispatchQueueAttributes // 實用工具優先級別(耗時操作,可以使用這個)
* public static let qosBackground: DispatchQueueAttributes // 后臺優先級
(只有最前的兩個牽扯到隊列的 串行和并行。 其余的都和線程的優先級設置有關)
- DispatchQueue : 隊列(沒有相關文字說明這是干什么的)
*/
public convenience init(label: String, attributes: DispatchQueueAttributes = default, target: DispatchQueue? = default)
2. 全局隊列的一些說明
/** 類方法提供了默認的參數,當我們給不傳參數的時候,使用的是默認參數;
attributes: DispatchQueue.GlobalAttributes = default :
*/
public class func global(attributes: DispatchQueue.GlobalAttributes = default) -> DispatchQueue // 獲取主隊列的參數 結構體
public struct GlobalAttributes : OptionSet {
public let rawValue: UInt64
public init(rawValue: UInt64)
// 服務質量
public static let qosUserInteractive: DispatchQueue.GlobalAttributes
public static let qosUserInitiated: DispatchQueue.GlobalAttributes
public static let qosDefault: DispatchQueue.GlobalAttributes
public static let qosUtility: DispatchQueue.GlobalAttributes
public static let qosBackground: DispatchQueue.GlobalAttributes
// 在 swift3.0 中已經廢棄了的 ,除了第一個以外和上面是一一對應的。
public static let priorityHigh: DispatchQueue.GlobalAttributes
public static let priorityDefault: DispatchQueue.GlobalAttributes
public static let priorityLow: DispatchQueue.GlobalAttributes
public static let priorityBackground: DispatchQueue.GlobalAttributes
}
// 這一段大家應該很熟悉。老版本的 objc 中 GCD 的文檔說明
* It is recommended to use quality of service class values to identify the
* 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:
* 全局并發隊列可能仍然通過 priority 進行表示。下面是 服務質量的映射:
* - 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
// 還有這一段
#define DISPATCH_QUEUE_PRIORITY_HIGH 2 (搞優先級, 值為 2)
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 (默認優先級, 值為 0)
#define DISPATCH_QUEUE_PRIORITY_LOW (-2) (低優先級, 值為 -2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN (后臺優先級,在高優先級執行完畢后值優先級的值 為 2)
在 swift 2.2 版本中 dispatch_sync(dispatch_get_global_queue(0, 0) // 基本可以看出這里為什么填 0 ,0 (第二個參數沒有啟用填任何數都可以,習慣填 0 )
文/瀟小濺(簡書作者)原文鏈接:http://www.lxweimin.com/p/f042432e2d7d著作權歸作者所有,轉載請聯系作者獲得授權,并標注“簡書作者”。
3. 獲取一個隊列
我們使用 DispatchQueue.global()
獲取一個系統的隊列,這樣的話獲取的就是默認.default
優先級的隊列了,如果要獲取其他優先級的隊列,就使用DispatchQueue.global(qos: .userInitiated)
,最后,我們使用 .async {}
來執行代碼:
DispatchQueue.global(qos: .userInitiated).async {
//your code here
}
4. 創建一個隊列
直接用 DispatchQueue
的初始化器來創建一個隊列。最簡單直接的辦法是這樣:
let queue = DispatchQueue(label: "myBackgroundQueue")
復雜一點?你可以指定優先級以及隊列類別:
let queue = DispatchQueue(label: "myBackgroundQueue", qos: .userInitiated, attributes: .concurrent)
然后把代碼放進去即可:
queue.async {
print("aaa")
}