iOS实现多个异步线程同步的操作
设置一个场景,有10个异步线程,当10个异步线程全部完成后,再执行某一项的操作.
使用dispatch_queue的并行队列. 相机而言的就有dispatch_group
dispatch_queue_tdispatchQueue =dispatch_queue_create("ted.queue.next",DISPATCH_QUEUE_CONCURRENT);
dispatch_group_tdispatchGroup =dispatch_group_create();
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
NSLog(@"dispatch_1");
});
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
NSLog(@"dspatch_2");
});
dispatch_group_notify(dispatchGroup,dispatch_get_main_queue(), ^(){
NSLog(@"全部执行完毕.");
});
NSLog(@"这个线程什么时候走?");
2015-11-03 10:51:31.753 xcodedemo[4895:2175050]这个线程什么时候走?
2015-11-03 10:51:31.754 xcodedemo[4895:2175095] dspatch_2
2015-11-03 10:51:31.754 xcodedemo[4895:2175094] dispatch_1
2015-11-03 10:51:31.821 xcodedemo[4895:2175050]全部执行完毕.
这是打印的log. 线程.dispatch_1和dspatch_2 是无序的.. 一目了然了