GCD的特點(diǎn)
GCD會(huì)自動(dòng)利用更多的CPU內(nèi)核
GCD自動(dòng)管理線程的生命周期(創(chuàng)建線程磕洪,調(diào)度任務(wù),銷毀線程等)
只需要告訴 GCD 想要如何執(zhí)行什么任務(wù)诫龙,不需要編寫任何線程管理代碼
1.GCD 相關(guān)概念
任務(wù)(block):就是需要執(zhí)行的代碼塊析显,以block 的方式呈現(xiàn)。
同步(sync): 在當(dāng)前線程中執(zhí)行签赃,會(huì)阻塞當(dāng)前線程谷异,不會(huì)開啟新的線程,所以只能一次執(zhí)行一個(gè)任務(wù)锦聊,前一個(gè)任務(wù)執(zhí)行完畢歹嘹,當(dāng)前線程才會(huì)執(zhí)行后一個(gè)任務(wù)
異步(async):不阻塞當(dāng)前線程,由GCD 決定是否開啟新的線程
隊(duì)列(queue):裝載待執(zhí)行任務(wù)的一種隊(duì)形結(jié)構(gòu)孔庭,和排隊(duì)上車一樣尺上,先進(jìn)先出。GCD 中有兩種隊(duì)列結(jié)構(gòu):串行隊(duì)列和并發(fā)隊(duì)列圆到。
串行隊(duì)列(serialQueue):隊(duì)列中的任務(wù)只能按順序依次執(zhí)行怎抛。
并發(fā)隊(duì)列(conCurrentQueue):隊(duì)列中的任務(wù)可以多個(gè)同時(shí)執(zhí)行,在async 下是真正的并發(fā)构资,在sync 下依然是串行,因?yàn)橹挥幸粭l線程可用陨簇。
GCD是將要執(zhí)行的代碼加入隊(duì)列中(串行隊(duì)列或并發(fā)隊(duì)列)吐绵,然后指定運(yùn)行方式(同步執(zhí)行或異步執(zhí)行)
2.創(chuàng)建隊(duì)列
GCD 需要把代碼塊放到隊(duì)列中,隊(duì)列又分串行隊(duì)列和并發(fā)隊(duì)列河绽。
第一個(gè)參數(shù) label 是隊(duì)列的標(biāo)識(shí)符己单,可以為空,第二個(gè)attributes 是表示串行或者并發(fā)隊(duì)列耙饰。默認(rèn)為串行隊(duì)列纹笼,.concurren 表示并發(fā)隊(duì)列。
// 串行隊(duì)列
let serialQueue = DispatchQueue(label: "dispatchSeralQueue")
//并發(fā)隊(duì)列
let conCurrentQueue = DispatchQueue(label: "dispatchConcurrentQueue", attributes: .concurrent)
除了自定義的隊(duì)列苟跪,系統(tǒng)還我們提供了兩個(gè)隊(duì)列
主隊(duì)列廷痘,主隊(duì)列是一個(gè)并發(fā)隊(duì)列。
DispatchQueue.main.async {
}
全局隊(duì)列件已,是一個(gè)并發(fā)隊(duì)列
DispatchQueue.global().async {
}
3.GCD使用
隊(duì)列和同步異步之間有多種組合方式
1.串行同步
2.串行異步
3.并發(fā)同步
4.并發(fā)異步
5.主隊(duì)列同步
6.主隊(duì)列異步
- 串行同步
let seralQueue = DispatchQueue(label: "dispatchSeralQueue")
seralQueue.sync {
for i in 0...3 {
debugPrint("queueTest seralQueue1: \(Thread.current), \(i)")
}
}
seralQueue.sync {
for i in 0...3 {
debugPrint("queueTest seralQueue2: \(Thread.current), \(i)")
}
}
"queueTest seralQueue1: <_NSMainThread: 0x600003db08c0>{number = 1, name = main}, 0"
"queueTest seralQueue1: <_NSMainThread: 0x600003db08c0>{number = 1, name = main}, 1"
"queueTest seralQueue1: <_NSMainThread: 0x600003db08c0>{number = 1, name = main}, 2"
"queueTest seralQueue1: <_NSMainThread: 0x600003db08c0>{number = 1, name = main}, 3"
"queueTest seralQueue2: <_NSMainThread: 0x600003db08c0>{number = 1, name = main}, 0"
"queueTest seralQueue2: <_NSMainThread: 0x600003db08c0>{number = 1, name = main}, 1"
"queueTest seralQueue2: <_NSMainThread: 0x600003db08c0>{number = 1, name = main}, 2"
"queueTest seralQueue2: <_NSMainThread: 0x600003db08c0>{number = 1, name = main}, 3"
結(jié)果笋额,順序執(zhí)行,都在主線程
- 串行異步
let seralQueue = DispatchQueue(label: "dispatchSeralQueue")
seralQueue.async {
for i in 0...3 {
debugPrint("queueTest seralQueue1: \(Thread.current), \(i)")
}
}
seralQueue.async {
for i in 0...3 {
debugPrint("queueTest seralQueue2: \(Thread.current), \(i)")
}
}
"queueTest seralQueue1: <NSThread: 0x6000030d2c40>{number = 6, name = (null)}, 0"
"queueTest seralQueue1: <NSThread: 0x6000030d2c40>{number = 6, name = (null)}, 1"
"queueTest seralQueue1: <NSThread: 0x6000030d2c40>{number = 6, name = (null)}, 2"
"queueTest seralQueue1: <NSThread: 0x6000030d2c40>{number = 6, name = (null)}, 3"
"queueTest seralQueue2: <NSThread: 0x6000030d2c40>{number = 6, name = (null)}, 0"
"queueTest seralQueue2: <NSThread: 0x6000030d2c40>{number = 6, name = (null)}, 1"
"queueTest seralQueue2: <NSThread: 0x6000030d2c40>{number = 6, name = (null)}, 2"
"queueTest seralQueue2: <NSThread: 0x6000030d2c40>{number = 6, name = (null)}, 3"
結(jié)果不在主線程篷扩,但是依然是順序執(zhí)行
- 并發(fā)同步
let conCurrentQueue = DispatchQueue(label: "dispatchConcurrentQueue", attributes: .concurrent)
conCurrentQueue.sync {
for i in 0...3 {
debugPrint("queueTest conCurrentQueue1: \(Thread.current), \(i)")
}
}
conCurrentQueue.sync {
for i in 0...3 {
debugPrint("queueTest conCurrentQueue2: \(Thread.current), \(i)")
}
}
"queueTest conCurrentQueue1: <_NSMainThread: 0x600000648380>{number = 1, name = main}, 0"
"queueTest conCurrentQueue1: <_NSMainThread: 0x600000648380>{number = 1, name = main}, 1"
"queueTest conCurrentQueue1: <_NSMainThread: 0x600000648380>{number = 1, name = main}, 2"
"queueTest conCurrentQueue1: <_NSMainThread: 0x600000648380>{number = 1, name = main}, 3"
"queueTest conCurrentQueue2: <_NSMainThread: 0x600000648380>{number = 1, name = main}, 0"
"queueTest conCurrentQueue2: <_NSMainThread: 0x600000648380>{number = 1, name = main}, 1"
"queueTest conCurrentQueue2: <_NSMainThread: 0x600000648380>{number = 1, name = main}, 2"
"queueTest conCurrentQueue2: <_NSMainThread: 0x600000648380>{number = 1, name = main}, 3"
在主線程兄猩,順序執(zhí)行
- 并發(fā)異步
let conCurrentQueue = DispatchQueue(label: "dispatchConcurrentQueue", attributes: .concurrent)
conCurrentQueue.async {
for i in 0...3 {
debugPrint("queueTest conCurrentQueue1: \(Thread.current), \(i)")
}
}
conCurrentQueue.async {
for i in 0...3 {
debugPrint("queueTest conCurrentQueue2: \(Thread.current), \(i)")
}
}
"queueTest conCurrentQueue2: <NSThread: 0x600003bf2900>{number = 4, name = (null)}, 0"
"queueTest conCurrentQueue1: <NSThread: 0x600003b98240>{number = 6, name = (null)}, 0"
"queueTest conCurrentQueue2: <NSThread: 0x600003bf2900>{number = 4, name = (null)}, 1"
"queueTest conCurrentQueue1: <NSThread: 0x600003b98240>{number = 6, name = (null)}, 1"
"queueTest conCurrentQueue2: <NSThread: 0x600003bf2900>{number = 4, name = (null)}, 2"
"queueTest conCurrentQueue1: <NSThread: 0x600003b98240>{number = 6, name = (null)}, 2"
"queueTest conCurrentQueue2: <NSThread: 0x600003bf2900>{number = 4, name = (null)}, 3"
"queueTest conCurrentQueue1: <NSThread: 0x600003b98240>{number = 6, name = (null)}, 3"
任務(wù)同時(shí)進(jìn)行,開啟多個(gè)線程。
- 主隊(duì)列同步和異步
http://www.reibang.com/p/253a3b572a0e枢冤,這篇文章已經(jīng)記錄鸠姨。 - GCD 線程之間通信
DispatchQueue.global().async {
// 模擬耗時(shí)操作
Thread.sleep(forTimeInterval: 10)
DispatchQueue.main.async {
// 在主隊(duì)列插入一個(gè)任務(wù)更新UI
}
}
-GCD 柵欄函數(shù)
當(dāng)任務(wù)需要異步進(jìn)行,但是這些任務(wù)需要分成兩組來執(zhí)行淹真,第一組完成之后才能進(jìn)行第二組的操作讶迁。這時(shí)候就用了到GCD的柵欄方法:
let dispathGroup = DispatchGroup()
let dispatchConcurrentQueue = DispatchQueue(label: "dispatchConcurrentQueue", attributes: .concurrent)
dispatchConcurrentQueue.async {
for i in 0...3 {
debugPrint("dispatchConcurrentQueue action1 \(i) at \(Thread.current)")
}
}
dispatchConcurrentQueue.async {
for i in 0...3 {
debugPrint("dispatchConcurrentQueue action2 \(i) at \(Thread.current)")
}
}
dispatchConcurrentQueue.async(group: dispathGroup, qos: .default, flags: .barrier) {
debugPrint("dispatchConcurrentQueue barrier")
}
dispatchConcurrentQueue.async {
for i in 0...3 {
debugPrint("dispatchConcurrentQueue action3 \(i) at \(Thread.current)")
}
}
dispatchConcurrentQueue.async {
for i in 0...3 {
debugPrint("dispatchConcurrentQueue action4 \(i) at \(Thread.current)")
}
}
"dispatchConcurrentQueue action2 0 at <NSThread: 0x6000002ab200>{number = 7, name = (null)}"
"dispatchConcurrentQueue action1 0 at <NSThread: 0x6000002aa580>{number = 4, name = (null)}"
"dispatchConcurrentQueue action2 1 at <NSThread: 0x6000002ab200>{number = 7, name = (null)}"
"dispatchConcurrentQueue action1 1 at <NSThread: 0x6000002aa580>{number = 4, name = (null)}"
"dispatchConcurrentQueue action2 2 at <NSThread: 0x6000002ab200>{number = 7, name = (null)}"
"dispatchConcurrentQueue action2 3 at <NSThread: 0x6000002ab200>{number = 7, name = (null)}"
"dispatchConcurrentQueue action1 2 at <NSThread: 0x6000002aa580>{number = 4, name = (null)}"
"dispatchConcurrentQueue action1 3 at <NSThread: 0x6000002aa580>{number = 4, name = (null)}"
"dispatchConcurrentQueue barrier"
"dispatchConcurrentQueue action3 0 at <NSThread: 0x6000002ab200>{number = 7, name = (null)}"
"dispatchConcurrentQueue action4 0 at <NSThread: 0x6000002aa580>{number = 4, name = (null)}"
"dispatchConcurrentQueue action3 1 at <NSThread: 0x6000002ab200>{number = 7, name = (null)}"
"dispatchConcurrentQueue action4 1 at <NSThread: 0x6000002aa580>{number = 4, name = (null)}"
"dispatchConcurrentQueue action3 2 at <NSThread: 0x6000002ab200>{number = 7, name = (null)}"
"dispatchConcurrentQueue action4 2 at <NSThread: 0x6000002aa580>{number = 4, name = (null)}"
"dispatchConcurrentQueue action3 3 at <NSThread: 0x6000002ab200>{number = 7, name = (null)}"
"dispatchConcurrentQueue action4 3 at <NSThread: 0x6000002aa580>{number = 4, name = (null)}"
- GCD 延遲執(zhí)行
func dispatchAfter() {
debugPrint("\(Date())")
DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
debugPrint("等了2 s\(Date())")
}
}
"2021-11-21 07:22:25 +0000"
"等了2 s2021-11-21 07:22:27 +0000"
- DispatchGroup
func dispatchGroupNotify() {
let dispatchGroup = DispatchGroup()
let seralQueue = DispatchQueue(label: "dispatchSeralSync")
seralQueue.async(group: dispatchGroup, execute: DispatchWorkItem(block: {
print("dispatchGroup do\(Thread.current)")
}))
seralQueue.async(group: dispatchGroup, execute: DispatchWorkItem(block: {
print("dispatchGroup do1\(Thread.current)")
}))
dispatchGroup.notify(queue: DispatchQueue.main, work: DispatchWorkItem(block: {
print("dispatchGroup.notify(queue: DispatchQueue.main\(Thread.current)")
}))
}
dispatchGroup do<NSThread: 0x6000016acbc0>{number = 4, name = (null)}
dispatchGroup do1<NSThread: 0x6000016acbc0>{number = 4, name = (null)}
dispatchGroup.notify(queue: DispatchQueue.main<_NSMainThread: 0x6000016e81c0>{number = 1, name = main}