iOS-GCD使用詳解及實(shí)例解析
iOS-GCD使用詳解
前言
對初學(xué)者來說,GCD似乎是一道邁不過去的坎,很多人在同步、異步、串行、并行和死鎖這幾個(gè)名詞的漩渦中漸漸放棄治療。本文將使用圖文表并茂的方式給大家形象地解釋其中的原理和規(guī)律。
線程、任務(wù)和隊(duì)列的概念
異步、同步 & 并行、串行的特點(diǎn)
一條重要的準(zhǔn)則
一般來說,我們使用GCD的最大目的是在新的線程中同時(shí)執(zhí)行多個(gè)任務(wù),這意味著我們需要兩項(xiàng)條件:
-
能開啟新的線程
-
任務(wù)可以同時(shí)執(zhí)行
-
結(jié)合以上兩個(gè)條件,也就等價(jià)“開啟新線程的能力 + 任務(wù)同步執(zhí)行的權(quán)利”,只有在滿足能力與權(quán)利這兩個(gè)條件的前提下,我們才可以在同時(shí)執(zhí)行多個(gè)任務(wù)。
-
所有組合的特點(diǎn)
(一)異步執(zhí)行 + 并行隊(duì)列
實(shí)現(xiàn)代碼:
//異步執(zhí)行 + 并行隊(duì)列 - (void)asyncConcurrent{ //創(chuàng)建一個(gè)并行隊(duì)列 dispatch_queue_t queue = dispatch_queue_create("標(biāo)識(shí)符", DISPATCH_QUEUE_CONCURRENT); NSLog(@"---start---"); //使用異步函數(shù)封裝三個(gè)任務(wù) dispatch_async(queue, ^{ NSLog(@"任務(wù)1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任務(wù)2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任務(wù)3---%@", [NSThread currentThread]); }); NSLog(@"---end---"); }
打印結(jié)果:
1 2 3 4 5 | ---start--- ---end--- 任務(wù)3---{number = 5, name = (null)} 任務(wù)2---{number = 4, name = (null)} 任務(wù)1---{number = 3, name = (null)} |
解釋
-
異步執(zhí)行意味著
-
可以開啟新的線程
-
任務(wù)可以先繞過不執(zhí)行,回頭再來執(zhí)行
-
-
并行隊(duì)列意味著
-
任務(wù)之間不需要排隊(duì),且具有同時(shí)被執(zhí)行的“權(quán)利”
-
-
兩者組合后的結(jié)果
-
開了三個(gè)新線程
-
函數(shù)在執(zhí)行時(shí),先打印了start和end,再回頭執(zhí)行這三個(gè)任務(wù)
-
這三個(gè)任務(wù)是同時(shí)執(zhí)行的,沒有先后,所以打印結(jié)果是“任務(wù)3-->任務(wù)2-->任務(wù)1”
-
步驟圖
(二)異步執(zhí)行 + 串行隊(duì)列
實(shí)現(xiàn)代碼:
//異步執(zhí)行 + 串行隊(duì)列 - (void)asyncSerial{ //創(chuàng)建一個(gè)串行隊(duì)列 dispatch_queue_t queue = dispatch_queue_create("標(biāo)識(shí)符", DISPATCH_QUEUE_SERIAL); NSLog(@"---start---"); //使用異步函數(shù)封裝三個(gè)任務(wù) dispatch_async(queue, ^{ NSLog(@"任務(wù)1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任務(wù)2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任務(wù)3---%@", [NSThread currentThread]); }); NSLog(@"---end---"); }
打印結(jié)果:
1 2 3 4 5 | ---start--- ---end--- 任務(wù)1---{number = 3, name = (null)} 任務(wù)2---{number = 3, name = (null)} 任務(wù)3---{number = 3, name = (null)} |
解釋
-
異步執(zhí)行意味著
-
可以開啟新的線程
-
任務(wù)可以先繞過不執(zhí)行,回頭再來執(zhí)行
-
-
串行隊(duì)列意味著
-
任務(wù)必須按添加進(jìn)隊(duì)列的順序挨個(gè)執(zhí)行
-
-
兩者組合后的結(jié)果
-
開了一個(gè)新的子線程
-
函數(shù)在執(zhí)行時(shí),先打印了start和end,再回頭執(zhí)行這三個(gè)任務(wù)
-
這三個(gè)任務(wù)是按順序執(zhí)行的,所以打印結(jié)果是“任務(wù)1-->任務(wù)2-->任務(wù)3”
-
步驟圖
(三)同步執(zhí)行 + 并行隊(duì)列
實(shí)現(xiàn)代碼:
//同步執(zhí)行 + 并行隊(duì)列 - (void)syncConcurrent{ //創(chuàng)建一個(gè)并行隊(duì)列 dispatch_queue_t queue = dispatch_queue_create("標(biāo)識(shí)符", DISPATCH_QUEUE_CONCURRENT); NSLog(@"---start---"); //使用同步函數(shù)封裝三個(gè)任務(wù) dispatch_sync(queue, ^{ NSLog(@"任務(wù)1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任務(wù)2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任務(wù)3---%@", [NSThread currentThread]); }); NSLog(@"---end---"); }
打印結(jié)果:
1 2 3 4 5 | ---start--- 任務(wù)1---{number = 1, name = main} 任務(wù)2---{number = 1, name = main} 任務(wù)3---{number = 1, name = main} ---end--- |
解釋
-
同步執(zhí)行執(zhí)行意味著
-
不能開啟新的線程
-
任務(wù)創(chuàng)建后必須執(zhí)行完才能往下走
-
-
并行隊(duì)列意味著
-
任務(wù)必須按添加進(jìn)隊(duì)列的順序挨個(gè)執(zhí)行
-
-
兩者組合后的結(jié)果
-
所有任務(wù)都只能在主線程中執(zhí)行
-
函數(shù)在執(zhí)行時(shí),必須按照代碼的書寫順序一行一行地執(zhí)行完才能繼續(xù)
-
-
注意事項(xiàng)
-
在這里即便是并行隊(duì)列,任務(wù)可以同時(shí)執(zhí)行,但是由于只存在一個(gè)主線程,所以沒法把任務(wù)分發(fā)到不同的線程去同步處理,其結(jié)果就是只能在主線程里按順序挨個(gè)挨個(gè)執(zhí)行了
-
步驟圖
(四)同步執(zhí)行+ 串行隊(duì)列
實(shí)現(xiàn)代碼:
- (void)syncSerial{ //創(chuàng)建一個(gè)串行隊(duì)列 dispatch_queue_t queue = dispatch_queue_create("標(biāo)識(shí)符", DISPATCH_QUEUE_SERIAL); NSLog(@"---start---"); //使用異步函數(shù)封裝三個(gè)任務(wù) dispatch_sync(queue, ^{ NSLog(@"任務(wù)1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任務(wù)2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任務(wù)3---%@", [NSThread currentThread]); }); NSLog(@"---end---"); }
打印結(jié)果:
1 2 3 4 5 | ---start--- 任務(wù)1---{number = 1, name = main} 任務(wù)2---{number = 1, name = main} 任務(wù)3---{number = 1, name = main} ---end--- |
解釋
-
這里的執(zhí)行原理和步驟圖跟“同步執(zhí)行+并發(fā)隊(duì)列”是一樣的,只要是同步執(zhí)行就沒法開啟新的線程,所以多個(gè)任務(wù)之間也一樣只能按順序來執(zhí)行,
(五)異步執(zhí)行+主隊(duì)列
實(shí)現(xiàn)代碼:
- (void)asyncMain{ //獲取主隊(duì)列 dispatch_queue_t queue = dispatch_get_main_queue(); NSLog(@"---start---"); //使用異步函數(shù)封裝三個(gè)任務(wù) dispatch_async(queue, ^{ NSLog(@"任務(wù)1---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任務(wù)2---%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"任務(wù)3---%@", [NSThread currentThread]); }); NSLog(@"---end---"); }
打印結(jié)果:
1 2 3 4 5 | ---start--- ---end--- 任務(wù)1---{number = 1, name = main} 任務(wù)2---{number = 1, name = main} 任務(wù)3---{number = 1, name = main} |
解釋
-
異步執(zhí)行意味著
-
可以開啟新的線程
-
任務(wù)可以先繞過不執(zhí)行,回頭再來執(zhí)行
-
-
主隊(duì)列跟串行隊(duì)列的區(qū)別
-
隊(duì)列中的任務(wù)一樣要按順序執(zhí)行
-
主隊(duì)列中的任務(wù)必須在主線程中執(zhí)行,不允許在子線程中執(zhí)行
-
-
以上條件組合后得出結(jié)果:
-
所有任務(wù)都可以先跳過,之后再來“按順序”執(zhí)行
-
步驟圖
(六)同步執(zhí)行+主隊(duì)列(死鎖)
實(shí)現(xiàn)代碼:
- (void)syncMain{ //獲取主隊(duì)列 dispatch_queue_t queue = dispatch_get_main_queue(); NSLog(@"---start---"); //使用同步函數(shù)封裝三個(gè)任務(wù) dispatch_sync(queue, ^{ NSLog(@"任務(wù)1---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任務(wù)2---%@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"任務(wù)3---%@", [NSThread currentThread]); }); NSLog(@"---end---"); }
打印結(jié)果:
1 | ---start--- |
解釋
-
主隊(duì)列中的任務(wù)必須按順序挨個(gè)執(zhí)行
-
任務(wù)1要等主線程有空的時(shí)候(即主隊(duì)列中的所有任務(wù)執(zhí)行完)才能執(zhí)行
-
主線程要執(zhí)行完“打印end”的任務(wù)后才有空
-
“任務(wù)1”和“打印end”兩個(gè)任務(wù)互相等待,造成死鎖
步驟圖
寫在結(jié)尾的話
以上就是我對GCD的基礎(chǔ)知識(shí)和幾種組合的理解。
相關(guān)文章
IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(二)
這篇文章主要介紹了IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能,點(diǎn)擊獲取驗(yàn)證碼,進(jìn)入時(shí)間倒計(jì)時(shí),感興趣的小伙伴們可以參考一下2016-04-04iOS中UILabel text兩邊對齊的實(shí)現(xiàn)代碼
本文通過一段實(shí)例代碼給大家介紹了ios中uilabel text兩邊對齊的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01IOS實(shí)現(xiàn)點(diǎn)擊滑動(dòng)抽屜效果
這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)點(diǎn)擊滑動(dòng)抽屜效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02iOS tableview實(shí)現(xiàn)簡單搜索功能
這篇文章主要為大家詳細(xì)介紹了iOS tableview實(shí)現(xiàn)簡單搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11