GCD学习之dispatch_barrier_async
? ? iOS常見的多線程開發方式有NSThread、NSOPeration和GCD,抽象程度依次提高,GCD是最抽象的,使用起來最簡單,但相對來說功能有限,比如不能cancel任務,這也算是一點遺憾吧。
? ? 今天主要記錄下學習dispatch_barrier_async中遇到的問題(其實是自己沒仔細看Apple文檔),dispatch_barrier_async是在執行完前面的任務后它才執行,而且它后面的任務等它執行完成之后才會執行(先后順序是按照添加到queue的次序),但該API適用的場景是dispatch queue必須是用DISPATCH_QUEUE_CONCURRENT屬性創建的queue,而不能是用系統定義好的dispatch_get_global_queue,下面是測試代碼:
1、當dispatch queue是dispatch_get_global_queue時,dispatch_barrier_async失效:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view, typically from a nib. 4 NSLog(@"main thread:%p",[NSThread currentThread]); 5 [self testBarrierBlockWithGlobalQueue]; 6 } 7 - (void)testBarrierBlockWithGlobalQueue 8 { 9 NSLog(@"current iOS Version:%.1f",[[[UIDevice currentDevice] systemVersion] floatValue]); 10 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 11 dispatch_async(queue, ^{ 12 [NSThread sleepForTimeInterval:2]; 13 NSLog(@"Excute block 1:%p",[NSThread currentThread]); 14 }); 15 dispatch_async(queue, ^{ 16 [NSThread sleepForTimeInterval:3]; 17 NSLog(@"Excute block 2:%p",[NSThread currentThread]); 18 }); 19 dispatch_barrier_async(queue, ^{ 20 NSLog(@"Excute block 3:%p",[NSThread currentThread]); 21 [NSThread sleepForTimeInterval:4]; 22 }); 23 dispatch_async(queue, ^{ 24 NSLog(@"Excute block 4:%p",[NSThread currentThread]); 25 }); 26 } 27 打印結果: 28 2015-06-09 23:16:33.799 testGCD[46920:3369866] main thread:0x7f87f8c0af70 29 2015-06-09 23:16:33.800 testGCD[46920:3369866] current iOS Version:8.3 30 2015-06-09 23:16:33.800 testGCD[46920:3370037] Excute block 4:0x7f87f8e14a60 31 2015-06-09 23:16:33.800 testGCD[46920:3370029] Excute block 3:0x7f87f8f222d0 32 2015-06-09 23:16:35.802 testGCD[46920:3370027] Excute block 1:0x7f87f8f27a90 33 2015-06-09 23:16:36.804 testGCD[46920:3370026] Excute block 2:0x7f87f8c1e610從打印結果可以看到不是預想的block3在block1、block2后面,最后再執行block4,說明在global_queue下dispatch_barrier_async失效。
2、當dispatch queue是用DISPATCH_QUEUE_CONCURRENT屬性創建的queue時,dispatch_barrier_async有效:
1 - (void)testBarrierBlockWithCreateQueue 2 { 3 NSLog(@"current iOS Version:%.1f",[[[UIDevice currentDevice] systemVersion] floatValue]); 4 dispatch_queue_t queue = dispatch_queue_create("com.testBarrierGCD", DISPATCH_QUEUE_CONCURRENT); 5 dispatch_async(queue, ^{ 6 [NSThread sleepForTimeInterval:2]; 7 NSLog(@"Excute block 1:%p",[NSThread currentThread]); 8 }); 9 dispatch_async(queue, ^{ 10 [NSThread sleepForTimeInterval:3]; 11 NSLog(@"Excute block 2:%p",[NSThread currentThread]); 12 }); 13 dispatch_barrier_async(queue, ^{ 14 NSLog(@"Excute block 3:%p",[NSThread currentThread]); 15 [NSThread sleepForTimeInterval:4]; 16 }); 17 dispatch_async(queue, ^{ 18 NSLog(@"Excute block 4:%p",[NSThread currentThread]); 19 }); 20 } 21 打印結果: 22 2015-06-09 23:21:55.671 testGCD[46963:3373180] main thread:0x7fa51a428130 23 2015-06-09 23:21:55.672 testGCD[46963:3373180] current iOS Version:8.3 24 2015-06-09 23:21:57.676 testGCD[46963:3373293] Excute block 1:0x7fa51a65a890 25 2015-06-09 23:21:58.673 testGCD[46963:3373294] Excute block 2:0x7fa51a71cf50 26 2015-06-09 23:21:58.674 testGCD[46963:3373294] Excute block 3:0x7fa51a71cf50 27 2015-06-09 23:22:02.676 testGCD[46963:3373294] Excute block 4:0x7fa51a71cf50從打印結果可以看出block執行順序是按照我們預期的順序在執行,至此說明了dispatch_barrier_async適用的場景是dispatch queue必須是用DISPATCH_QUEUE_CONCURRENT屬性創建的queue(其實Apple文檔中有說明的,以后用新的API時一定得仔細閱讀文檔,避免多走彎路)~祝大家玩的愉快~
測試代碼已上傳至GitHub:https://github.com/iOSGeek0829/testGCDWithBarrierBlock
轉載于:https://www.cnblogs.com/NerdFooProgrammer/p/4564961.html
總結
以上是生活随笔為你收集整理的GCD学习之dispatch_barrier_async的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java之IO流学习总结【上】
- 下一篇: wampserver 绑定域名 外部可以