GCD作为日常开发中的使用已经非常普遍,基于C的API为应用在多核硬件上高效运行提供了有力支持。本文分场景写了几个测试Demo,方便大家理解与应用。
1,dispatch_get_global_queue与dispatch_get_main_queue交互
很多应用场景需要后台读写大量数据,通过dispatch_get_global_queue函数可以获取全局队列来并发执行后台任务,并再结束后更新UI,保证应用的流程,避免主线程阻塞。代码如下:
- (void)test1 { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create(); for (int i = 0; i < 10; i++) { dispatch_group_async(group, queue, ^{ NSLog(@"___%i %@", i, [NSThread currentThread]); }); } dispatch_group_notify(group, queue, ^{ NSLog(@"currentThread1 %@",[NSThread currentThread]); NSLog(@"执行完毕"); dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"currentThread2 %@",[NSThread currentThread]); NSLog(@"main_queue 执行完毕"); }); }); } |
执行结果:
test1:
2016-03-10 17:21:37.293 GCDSample[3892:2238505] ___0 <NSThread: 0x12e53e570>{number = 2, name = (null)}
2016-03-10 17:21:37.293 GCDSample[3892:2238506] ___2 <NSThread: 0x12e67f1e0>{number = 4, name = (null)}
2016-03-10 17:21:37.293 GCDSample[3892:2238504] ___3 <NSThread: 0x12e689760>{number = 3, name = (null)}
2016-03-10 17:21:37.293 GCDSample[3892:2238507] ___1 <NSThread: 0x12e683590>{number = 5, name = (null)}
2016-03-10 17:21:37.294 GCDSample[3892:2238505] ___4 <NSThread: 0x12e53e570>{number = 2, name = (null)}
2016-03-10 17:21:37.295 GCDSample[3892:2238506] ___5 <NSThread: 0x12e67f1e0>{number = 4, name = (null)}
2016-03-10 17:21:37.295 GCDSample[3892:2238504] ___6 <NSThread: 0x12e689760>{number = 3, name = (null)}
2016-03-10 17:21:37.295 GCDSample[3892:2238507] ___7 <NSThread: 0x12e683590>{number = 5, name = (null)}
2016-03-10 17:21:37.295 GCDSample[3892:2238505] ___8 <NSThread: 0x12e53e570>{number = 2, name = (null)}
2016-03-10 17:21:37.298 GCDSample[3892:2238506] ___9 <NSThread: 0x12e67f1e0>{number = 4, name = (null)}
2016-03-10 17:21:37.299 GCDSample[3892:2238506] currentThread1 <NSThread: 0x12e67f1e0>{number = 4, name = (null)}
2016-03-10 17:21:37.299 GCDSample[3892:2238506] 执行完毕
2016-03-10 17:21:37.302 GCDSample[3892:2238481] currentThread2 <NSThread: 0x12e50c390>{number = 1, name = main}
2016-03-10 17:21:37.303 GCDSample[3892:2238481] main_queue 执行完毕
2,异步线程中串行执行任务
test1中dispatch_group_async以并发的方式开启异步线程,不能保证执行顺序,如果想在并发线程中串行执行任务该如何做呢?只需要创建一个串行队列,加入group任务即可。
- (void)test1 { NSLog(@"code begin"); NSLog(@"currentThread0 %@",[NSThread currentThread]); dispatch_group_t group = dispatch_group_create(); dispatch_queue_t serialQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); for (int i = 0; i < 10; i++) { dispatch_group_async(group, serialQueue, ^{ [NSThread sleepForTimeInterval:.1f]; NSLog(@"___%i %@", i, [NSThread currentThread]); }); } dispatch_group_notify(group, serialQueue, ^{ NSLog(@"currentThread1 %@",[NSThread currentThread]); NSLog(@"执行完毕"); }); NSLog(@"code end"); } |
执行结果:
test2:
2016-03-10 17:07:10.366 GCDSample[3879:2235420] code begin
2016-03-10 17:07:10.367 GCDSample[3879:2235420] currentThread0 <NSThread: 0x14c50c350>{number = 1, name = main}
2016-03-10 17:07:10.367 GCDSample[3879:2235420] code end
2016-03-10 17:07:10.472 GCDSample[3879:2235438] ___0 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.577 GCDSample[3879:2235438] ___1 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.683 GCDSample[3879:2235438] ___2 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.784 GCDSample[3879:2235438] ___3 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.890 GCDSample[3879:2235438] ___4 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:10.995 GCDSample[3879:2235438] ___5 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.101 GCDSample[3879:2235438] ___6 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.206 GCDSample[3879:2235438] ___7 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.309 GCDSample[3879:2235438] ___8 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.411 GCDSample[3879:2235438] ___9 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.411 GCDSample[3879:2235438] currentThread1 <NSThread: 0x14c692ed0>{number = 2, name = (null)}
2016-03-10 17:07:11.411 GCDSample[3879:2235438] 执行完毕
3,dispatch_barrier_async,分割多任务线程
如果有10个并发任务,想要分成两组,比如指定前五个任务执行完之后,优先执行第六个任务,再执行剩下的操作,可以通过dispatch_barrier_async来分割开,示例:
- (void)test3 { dispatch_queue_t concurrentQueue = ({ dispatch_queue_t queue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT); queue; }); for (int i = 0; i < 10; i++) { if (i != 5) { dispatch_async(concurrentQueue, ^{ [NSThread sleepForTimeInterval:arc4random_uniform(3)]; NSLog(@"dispatch_async %i", i); }); }else{ dispatch_barrier_async(concurrentQueue, ^{ NSLog(@"dispatch_barrier_async"); }); } } } |
执行结果:
test3
2016-03-10 17:47:32.397 GCDSample[3949:2246479] dispatch_async 1
2016-03-10 17:47:32.400 GCDSample[3949:2246481] dispatch_async 3
2016-03-10 17:47:33.403 GCDSample[3949:2246480] dispatch_async 2
2016-03-10 17:47:34.400 GCDSample[3949:2246477] dispatch_async 0
2016-03-10 17:47:34.405 GCDSample[3949:2246479] dispatch_async 4
2016-03-10 17:47:34.406 GCDSample[3949:2246477] dispatch_barrier_async
2016-03-10 17:47:34.406 GCDSample[3949:2246479] dispatch_async 6
2016-03-10 17:47:35.411 GCDSample[3949:2246479] dispatch_async 7
2016-03-10 17:47:35.412 GCDSample[3949:2246480] dispatch_async 9
2016-03-10 17:47:35.411 GCDSample[3949:2246477] dispatch_async 8
4,串行队列死锁
- (void)test4 { NSLog(@"1"); dispatch_sync(dispatch_get_main_queue(), ^{ NSLog(@"2"); }); NSLog(@"3"); } - (void)test4_1 { NSLog(@"1"); dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"2 %@",[NSThread currentThread]); dispatch_sync(dispatch_get_main_queue(), ^{ NSLog(@"3 %@",[NSThread currentThread]); }); NSLog(@"4 %@",[NSThread currentThread]); }); NSLog(@"5"); } |
方法test4会造成死锁,因为main queue需要等待dispatch_sync函数中block返回才能继续执行,而通过dispatch_sync放入main queue的block按照FIFO的原则(先入先出)现在得不到执行,就造成了相互等待的局面,产生死锁。将同步的串行队列放到另外一个异步线程就能够解决(如方法test4_1所示)。所以在使用dispatch_sync的时候需要很谨慎,需要先执行[NSThread isMainThread]判断下当前任务是否在mian queue中调用,比如有一段代码在后台执行,而它需要从界面控制层获取一个值。那么你可以使用dispatch_sync简单办到。执行结果:
test4
2016-03-10 17:23:30.020 GCDSample[3896:2239119] 1
test4_1
2016-03-11 17:30:11.900 GCDSample[1197:730552] 1
2016-03-11 17:30:11.901 GCDSample[1197:730552] 5
2016-03-11 17:30:11.903 GCDSample[1197:730564] 2 <NSThread: 0x170263080>{number = 2, name = (null)}
2016-03-11 17:30:11.933 GCDSample[1197:730552] 3 <NSThread: 0x174075400>{number = 1, name = main}
2016-03-11 17:30:11.933 GCDSample[1197:730564] 4 <NSThread: 0x170263080>{number = 2, name = (null)}