歡迎私信探討。
-
GCD中隊(duì)列分為兩種
-
串行隊(duì)列:
串行隊(duì)列的特點(diǎn)是隊(duì)列中的任務(wù)會(huì)一個(gè)一個(gè)的執(zhí)行,前一個(gè)任務(wù)不執(zhí)行完成下一個(gè)任務(wù)就不會(huì)執(zhí)行。
創(chuàng)建方式:
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_SERIAL);
-
并發(fā)隊(duì)列:
并發(fā)隊(duì)列的特點(diǎn)是不管你多少的任務(wù)都會(huì)同時(shí)執(zhí)行,任務(wù)一般并發(fā)隊(duì)列和異步方式配對(duì)使用。
創(chuàng)建方式:
dispatch_queue_t queue = dispatch_queue_create("并發(fā)", DISPATCH_QUEUE_CONCURRENT);
-
-
線程執(zhí)行方式也分為兩種
-
同步執(zhí)行:
同步執(zhí)行的特點(diǎn)是,不具備開啟新線程能力,且會(huì)阻塞當(dāng)前線程。
調(diào)用方式:
dispatch_sync(<#dispatch_queue_t _Nonnull queue#>, <#^(void)block#>)
-
異步執(zhí)行:
異步執(zhí)行的特點(diǎn)是,具備開啟新線程能力,不會(huì)阻塞當(dāng)前線程;但是只是說它具備這個(gè)能力,并不是一定會(huì)開啟子線程,因?yàn)樵谥麝?duì)列中異步執(zhí)行依然會(huì)是在主線程中執(zhí)行。
調(diào)用方式:
dispatch_async(<#dispatch_queue_t _Nonnull queue#>, <#^(void)block#>)
-
接下來將通過串行并發(fā)同步異步穿插介紹其中原理。無特殊說明默認(rèn)在主線程中
serial sync 執(zhí)行
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
NSLog(@"0000");
輸出:
2019-06-19 17:49:34.547 fasfsdfs[21016:325617] 開始----<NSThread: 0x6000018ac140>{number = 1, name = main}
2019-06-19 17:49:36.548 fasfsdfs[21016:325617] 結(jié)束----<NSThread: 0x6000018ac140>{number = 1, name = main}
2019-06-19 17:49:36.548 fasfsdfs[21016:325617] 0000
分析
- 打印第一句:因?yàn)槭峭剑詴?huì)阻塞主線程去執(zhí)行 queue 隊(duì)列中的任務(wù),輸出第一句,由打印
0000
的時(shí)間和打印開始----
的時(shí)間對(duì)比可得。 - 接下來的輸出都是按部就班的打印了,沒有什么好深入的。
serial sync 任務(wù)中嵌套 sync 任務(wù)
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"111----%@",[NSThread currentThread]);
});
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
NSLog(@"0000");
輸出:
2019-06-19 18:11:26.510 fasfsdfs[21508:339513] 開始----<NSThread: 0x600003ce1540>{number = 1, name = main}
分析:
- 輸出
開始---
是正確的,上面分析過 - 如果你對(duì)死鎖有了解或者跑了代碼就會(huì)發(fā)現(xiàn),程序會(huì)
crash
,因?yàn)樵斐闪司€程死鎖。這一點(diǎn)需要重點(diǎn)講解一下。
這里發(fā)生死鎖的原因是:在串行隊(duì)列中同步任務(wù)中嵌套了一個(gè)新的同步任務(wù)。
根據(jù)串行隊(duì)列中任務(wù)是一個(gè)一個(gè)的執(zhí)行,同步執(zhí)行是會(huì)阻塞線程的,當(dāng)執(zhí)行到第二個(gè)dispatch_sync
時(shí),它阻塞線程去等待第一個(gè)dispatch_sync
里面的任務(wù)先去執(zhí)行完,而第二個(gè)同步任務(wù)卻是在第一個(gè)同步任務(wù)里面,只有第二個(gè)同步執(zhí)行完了才會(huì)繼續(xù)執(zhí)行接下來的代碼,這樣你看著我,我看著你,雙方都卡住了,就造成了死鎖。
serial sync 任務(wù)中嵌套 async 任務(wù)
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"111----%@",[NSThread currentThread]);
});
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
輸出:
2019-06-19 18:37:54.614 fasfsdfs[21713:343984] 開始----<NSThread: 0x6000024b94c0>{number = 1, name = main}
2019-06-19 18:37:56.615 fasfsdfs[21713:343984] 結(jié)束----<NSThread: 0x6000024b94c0>{number = 1, name = main}
2019-06-19 18:37:56.615 fasfsdfs[21713:355085] 111----<NSThread: 0x600002426640>{number = 14, name = (null)}
分析:
- 串行任務(wù)依次執(zhí)行,當(dāng)執(zhí)行到 async 時(shí)需要等當(dāng)前的任務(wù)執(zhí)行完成才回去執(zhí)行里面的任務(wù),因?yàn)槭钱惒降乃圆粫?huì)阻塞線程,且不是在主隊(duì)列中所以會(huì)開起子線程執(zhí)行,綜上所述不會(huì)造成死鎖。
serial async 任務(wù)中嵌套 sync
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"111----%@",[NSThread currentThread]);
});
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
NSLog(@"0000");
輸出:
2019-06-20 16:44:02.738 fasfsdfs[34323:745553] 開始----<NSThread: 0x600003558e80>{number = 5, name = (null)}
分析:
- 雖然是在異步調(diào)用的子線程中執(zhí)行
sync
任務(wù),但是依然會(huì)在調(diào)用sync
時(shí)crash
。原理同上,串行隊(duì)列任務(wù)一個(gè)個(gè)執(zhí)行,上一個(gè)任務(wù)不執(zhí)行完成,下一個(gè)任務(wù)就不會(huì)執(zhí)行,而sync
屬于任務(wù)嵌套中的任務(wù)。
serial async 任務(wù)中嵌套 async
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"111----%@",[NSThread currentThread]);
});
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
輸出:
2019-06-20 17:21:28.635 fasfsdfs[35506:772778] 開始----<NSThread: 0x6000001dde40>{number = 7, name = (null)}
2019-06-20 17:21:30.639 fasfsdfs[35506:772778] 結(jié)束----<NSThread: 0x6000001dde40>{number = 7, name = (null)}
2019-06-20 17:21:30.639 fasfsdfs[35506:772778] 111----<NSThread: 0x6000001dde40>{number = 7, name = (null)}
分析:
- 串行任務(wù),任務(wù)依次執(zhí)行,異步不會(huì)阻塞線程,具備開啟線程能力,所以在第二個(gè)
async
時(shí)會(huì)等當(dāng)前任務(wù)執(zhí)行完成再去執(zhí)行,由打印的時(shí)間和次序?qū)Ρ戎С忠陨辖Y(jié)論。
concurrent sync
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
NSLog(@"0000");
輸出:
2019-06-20 17:30:12.118 fasfsdfs[35506:772690] 開始----<NSThread: 0x60000014ee00>{number = 1, name = main}
2019-06-20 17:30:14.118 fasfsdfs[35506:772690] 結(jié)束----<NSThread: 0x60000014ee00>{number = 1, name = main}
2019-06-20 17:30:14.118 fasfsdfs[35506:772690] 0000
分析:
- 并發(fā)隊(duì)列任務(wù)可以同時(shí)執(zhí)行,同步不具備開啟線程能力,會(huì)阻塞當(dāng)前線程,由打印數(shù)據(jù)可以得出結(jié)論。
concurrent sync 任務(wù)中嵌套 sync
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"111----%@",[NSThread currentThread]);
});
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
輸出:
2019-06-20 17:32:13.585 fasfsdfs[35506:772690] 開始----<NSThread: 0x60000014ee00>{number = 1, name = main}
2019-06-20 17:32:13.585 fasfsdfs[35506:772690] 111----<NSThread: 0x60000014ee00>{number = 1, name = main}
2019-06-20 17:32:15.586 fasfsdfs[35506:772690] 結(jié)束----<NSThread: 0x60000014ee00>{number = 1, name = main}
分析:
- 并發(fā)與串行的差別在這里可以提現(xiàn)到淋漓盡致。如果串行這樣執(zhí)行,必然會(huì)造成死鎖。
- 執(zhí)行第一個(gè)
sync
時(shí),阻塞主線程,暫停正在執(zhí)行的任務(wù),轉(zhuǎn)而去執(zhí)行sync
里面的任務(wù)。 - 執(zhí)行到第二個(gè)
sync
時(shí),如果是串行任務(wù)之間會(huì)互相等待,而并發(fā)卻不會(huì),新任務(wù)不必等待之前的任務(wù)執(zhí)行完;所以第二個(gè) sync會(huì)暫停當(dāng)前任務(wù)去執(zhí)行自己block 里面的任務(wù)。 - 任務(wù)二執(zhí)行完成,主線程繼續(xù)執(zhí)行接下來的任務(wù),等待兩秒,打印輸出。
concurrent sync 任務(wù)中嵌套 async
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"111----%@",[NSThread currentThread]);
sleep(1);
});
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
輸出:
2019-06-20 22:21:33.544 fasfsdfs[35506:772690] 開始----<NSThread: 0x60000014ee00>{number = 1, name = main}
2019-06-20 22:21:33.544 fasfsdfs[35506:811697] 111----<NSThread: 0x600000130c80>{number = 11, name = (null)}
2019-06-20 22:21:35.545 fasfsdfs[35506:772690] 結(jié)束----<NSThread: 0x60000014ee00>{number = 1, name = main}
分析:
- 如果前面所講您已經(jīng)了解明白,我相信這一步不會(huì)是問題的。
- sync 主線程執(zhí)行,async 并發(fā)隊(duì)列開啟子線程執(zhí)行新任務(wù)
#######concurrent async
dispatch_queue_t queue = dispatch_queue_create("----", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
NSLog(@"0000");
輸出:
2019-06-20 22:26:57.317 fasfsdfs[35506:772690] 0000
2019-06-20 22:26:57.317 fasfsdfs[35506:822749] 開始----<NSThread: 0x6000001dc140>{number = 17, name = (null)}
2019-06-20 22:26:59.318 fasfsdfs[35506:822749] 結(jié)束----<NSThread: 0x6000001dc140>{number = 17, name = (null)}
分析:
- "0000" 打印在 "開始" 之前,我猜測(cè)可能是系統(tǒng)內(nèi)部要?jiǎng)?chuàng)建開辟內(nèi)存耗時(shí)了。
- 異步開啟子線程
concurrent async 任務(wù)中嵌套 sync
dispatch_async(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
dispatch_sync(queue, ^{
NSLog(@"111----%@",[NSThread currentThread]);
sleep(1);
});
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
輸出:
2019-06-20 22:33:16.687 fasfsdfs[35506:825627] 開始----<NSThread: 0x600000130500>{number = 20, name = (null)}
2019-06-20 22:33:16.687 fasfsdfs[35506:825627] 111----<NSThread: 0x600000130500>{number = 20, name = (null)}
2019-06-20 22:33:19.694 fasfsdfs[35506:825627] 結(jié)束----<NSThread: 0x600000130500>{number = 20, name = (null)}
分析:
- async 開啟子線程,sync 阻塞線程,去執(zhí)行自己的任務(wù)。
concurrent async 任務(wù)中嵌套 async
dispatch_async(queue, ^{
//打印 NSThread為main
NSLog(@"開始----%@",[NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"111----%@",[NSThread currentThread]);
sleep(1);
});
sleep(2);
NSLog(@"結(jié)束----%@",[NSThread currentThread]);
});
輸出:
2019-06-20 22:37:48.393 fasfsdfs[35506:830404] 開始----<NSThread: 0x6000001dee80>{number = 21, name = (null)}
2019-06-20 22:37:48.394 fasfsdfs[35506:830834] 111----<NSThread: 0x6000001def40>{number = 22, name = (null)}
2019-06-20 22:37:50.397 fasfsdfs[35506:830404] 結(jié)束----<NSThread: 0x6000001dee80>{number = 21, name = (null)}
分析:
- 執(zhí)行到第一個(gè) async,因?yàn)槭遣l(fā),開啟一個(gè)新線程去執(zhí)行任務(wù)。
- 執(zhí)行到第二個(gè) async,又開啟新的線程去執(zhí)行新的任務(wù)。
上面就是 并發(fā)/串行, 同步/異步 的各種嵌套分析;如果還有不清楚的讀者歡迎私信我。