詳解IOS串行隊(duì)列與并行隊(duì)列進(jìn)行同步或者異步的實(shí)例
詳解IOS串行隊(duì)列與并行隊(duì)列進(jìn)行同步或者異步的實(shí)例
IOS中GCD的隊(duì)列分為串行隊(duì)列和并行隊(duì)列,任務(wù)分為同步任務(wù)和異步任務(wù),他們的排列組合有四種情況,下面分析這四種情況的工作方式。
同步任務(wù),使用GCD dispatch_sync 進(jìn)行派發(fā)任務(wù)
- (void)testSync { dispatch_queue_t serialQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_SERIAL); dispatch_queue_t concurrentQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_CONCURRENT); NSLog(@"====serialQueue===="); for (int i = 0; i<10; i++) { dispatch_sync(serialQueue, ^{ [NSThread sleepForTimeInterval:0.3]; NSLog(@"==>%@ sync serial XXX>%d", [NSThread currentThread], i); }); } NSLog(@"====concurrentQueue===="); for (int i = 0; i<10; i++) { dispatch_sync(concurrentQueue, ^{ [NSThread sleepForTimeInterval:0.3]; NSLog(@"==>%@ sync concurrent ====>%d", [NSThread currentThread], i*i); }); } }
結(jié)果如下:
2017-03-01 01:36:22.835 Demo ====serialQueue==== 2017-03-01 01:36:23.207 {number = 1, name = main} sync serial XXX>0 2017-03-01 01:36:23.578 {number = 1, name = main} sync serial XXX>1 2017-03-01 01:36:23.952 {number = 1, name = main} sync serial XXX>2 2017-03-01 01:36:24.325 {number = 1, name = main} sync serial XXX>3 2017-03-01 01:36:24.699 {number = 1, name = main} sync serial XXX>4 2017-03-01 01:36:25.072 {number = 1, name = main} sync serial XXX>5 2017-03-01 01:36:25.446 {number = 1, name = main} sync serial XXX>6 2017-03-01 01:36:25.746 {number = 1, name = main} sync serial XXX>7 2017-03-01 01:36:26.122 {number = 1, name = main} sync serial XXX>8 2017-03-01 01:36:26.489 {number = 1, name = main} sync serial XXX>9 2017-03-01 01:36:26.489 Demo ====concurrentQueue==== 2017-03-01 01:36:26.864 {number = 1, name = main} sync concurrent ====>0 2017-03-01 01:36:27.236 {number = 1, name = main} sync concurrent ====>1 2017-03-01 01:36:27.611 {number = 1, name = main} sync concurrent ====>4 2017-03-01 01:36:27.985 {number = 1, name = main} sync concurrent ====>9 2017-03-01 01:36:28.354 {number = 1, name = main} sync concurrent ====>16 2017-03-01 01:36:28.726 {number = 1, name = main} sync concurrent ====>25 2017-03-01 01:36:29.100 {number = 1, name = main} sync concurrent ====>36 2017-03-01 01:36:29.474 {number = 1, name = main} sync concurrent ====>49 2017-03-01 01:36:29.849 {number = 1, name = main} sync concurrent ====>64 2017-03-01 01:36:30.223 {number = 1, name = main} sync concurrent ====>81
testSync方法是在主線程中調(diào)用的,結(jié)果看到使用的串行隊(duì)列和使用并行隊(duì)列看到的結(jié)果都是發(fā)生在當(dāng)前線程:主線程中,沒有開啟新的線程處理任務(wù),任務(wù)的調(diào)度也是串行調(diào)度的。
異步任務(wù),使用GCD dispatch_async 進(jìn)行派發(fā)任務(wù)
- (void)testAsync { dispatch_queue_t serialQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_SERIAL); dispatch_queue_t concurrentQueue = dispatch_queue_create("com.zyt.queue", DISPATCH_QUEUE_CONCURRENT); NSLog(@"====serialQueue===="); for (int i = 0; i<10; i++) { dispatch_async(serialQueue, ^{ [NSThread sleepForTimeInterval:0.3]; NSLog(@"==>%@ async serial XXX>%d", [NSThread currentThread], i); }); } NSLog(@"====concurrentQueue===="); for (int i = 0; i<10; i++) { dispatch_async(concurrentQueue, ^{ [NSThread sleepForTimeInterval:0.3]; NSLog(@"==>%@ async concurrent ====>%d", [NSThread currentThread], i*i); }); } } `
結(jié)果如下:
2017-03-01 01:45:36.125 Demo ====serialQueue==== 2017-03-01 01:45:36.125 Demo ====concurrentQueue==== 2017-03-01 01:45:36.494 {number = 3, name = (null)} async concurrent ====>0 2017-03-01 01:45:36.494 {number = 5, name = (null)} async concurrent ====>4 2017-03-01 01:45:36.494 {number = 4, name = (null)} async concurrent ====>1 2017-03-01 01:45:36.494 {number = 6, name = (null)} async concurrent ====>16 2017-03-01 01:45:36.494 {number = 8, name = (null)} async serial XXX>0 2017-03-01 01:45:36.494 {number = 7, name = (null)} async concurrent ====>9 2017-03-01 01:45:36.494 {number = 9, name = (null)} async concurrent ====>25 2017-03-01 01:45:36.494 {number = 11, name = (null)} async concurrent ====>49 2017-03-01 01:45:36.494 {number = 10, name = (null)} async concurrent ====>36 2017-03-01 01:45:36.501 {number = 13, name = (null)} async concurrent ====>81 2017-03-01 01:45:36.501 {number = 12, name = (null)} async concurrent ====>64 2017-03-01 01:45:36.869 {number = 8, name = (null)} async serial XXX>1 2017-03-01 01:45:37.244 {number = 8, name = (null)} async serial XXX>2 2017-03-01 01:45:37.615 {number = 8, name = (null)} async serial XXX>3 2017-03-01 01:45:37.986 {number = 8, name = (null)} async serial XXX>4 2017-03-01 01:45:38.358 {number = 8, name = (null)} async serial XXX>5 2017-03-01 01:45:38.730 {number = 8, name = (null)} async serial XXX>6 2017-03-01 01:45:39.103 {number = 8, name = (null)} async serial XXX>7 2017-03-01 01:45:39.472 {number = 8, name = (null)} async serial XXX>8 2017-03-01 01:45:39.842 {number = 8, name = (null)} async serial XXX>9
testSync方法是在主線程中調(diào)用的,結(jié)果看到使用的串行隊(duì)列的異步任務(wù)會開啟一個(gè)子線程執(zhí)行任務(wù),任務(wù)的調(diào)度是串行的
使用并行隊(duì)列的異步任務(wù)會開啟多個(gè)子線程并行的處理任務(wù),任務(wù)的先后順序是不固定的,任務(wù)的調(diào)度方式是并行的
總結(jié)
同步任務(wù):和使用的隊(duì)列無關(guān),不會開啟子線程處理任務(wù),會在當(dāng)前的線程中串行的調(diào)度任務(wù),即一個(gè)任務(wù)完成之后繼續(xù)下一個(gè)任務(wù),如果同步任務(wù)在主線程中調(diào)用,會阻塞主線程
異步任務(wù):a. 使用串行隊(duì)列,會開啟一個(gè)子線程串行的調(diào)度任務(wù) b. 使用并行隊(duì)列,會開啟多個(gè)子線程并行的調(diào)度任務(wù),這種情況用的是最多的。
以上就是對詳解IOS串行隊(duì)列與并行隊(duì)列進(jìn)行同步或者異步的實(shí)例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
iOS開發(fā)中一些手寫控件及其相關(guān)屬性的使用
這篇文章主要介紹了iOS開發(fā)中一些手寫控件及其相關(guān)屬性的使用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS開發(fā)中使用NSURLConnection類處理網(wǎng)絡(luò)請求的方法
這篇文章主要介紹了iOS開發(fā)中使用NSURLConnection類處理網(wǎng)絡(luò)請求的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS微信分享后關(guān)閉發(fā)送成功提示并返回應(yīng)用
這篇文章主要為大家詳細(xì)介紹了iOS微信分享后關(guān)閉發(fā)送成功提示并返回應(yīng)用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09在iOS應(yīng)用中使用UIWebView創(chuàng)建簡單的網(wǎng)頁瀏覽器界面
這篇文章主要介紹了在iOS應(yīng)用中使用UIWebView創(chuàng)建簡單的網(wǎng)頁瀏覽器界面的方法,包括動(dòng)態(tài)獲取UIWebView高度的實(shí)現(xiàn),需要的朋友可以參考下2016-01-01iOS中的多線程如何按設(shè)定順序去執(zhí)行任務(wù)詳解
多線程相信大家或多或少都有所了解吧,下面這篇文章主要給大家介紹了關(guān)于iOS中多線程如何按設(shè)定順序去執(zhí)行任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-12-12iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法
這篇文章主要介紹了iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01iOS CAEmitterLayer實(shí)現(xiàn)粒子發(fā)射動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了iOS CAEmitterLayer 實(shí)現(xiàn)粒子發(fā)射動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06iOS開發(fā)實(shí)現(xiàn)UIImageView的分類
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實(shí)現(xiàn)UIImageView的分類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01