void
dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block);
#dispatch_group_t group 执行任务的调度组
#dispatch_queue_t queue 执行任务的队列
#dispatch_block_t block 被执行的任务
简单使用dispatch_group_async
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queueA = dispatch_queue_create("queueA", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queueA, ^{
for (int i=0; i<3; i++) {
sleep(1);
NSLog(@"queueA i = %d", i);
}
});
dispatch_group_async(group, queueB, ^{
for (int n=0; n<3; n++) {
sleep(1);
NSLog(@"queueB n = %d", n);
}
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"all finished");
});
输出结果
很明显,在group中的队列,任务都执行完毕了,才会执行dispatch_group_notify里的任务。
dispatch_group_enter + dispatch_group_leave + dispatch_group_notify
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(10, queue, ^(size_t n) {
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
int i = arc4random()%10;
sleep(i);
NSLog(@"%zuth thread sleep %d seconds",n,i);
dispatch_group_leave(group);
});
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"all finished");
});
输出结果
2018-12-01 20:54:53.573643+0800 dispatch_group-Demo[47564:2176026] 7th thread sleep 0 seconds
2018-12-01 20:54:54.576867+0800 dispatch_group-Demo[47564:2176021] 0th thread sleep 1 seconds
2018-12-01 20:54:55.575458+0800 dispatch_group-Demo[47564:2176025] 6th thread sleep 2 seconds
2018-12-01 20:54:56.575312+0800 dispatch_group-Demo[47564:2176027] 8th thread sleep 3 seconds
2018-12-01 20:54:57.575330+0800 dispatch_group-Demo[47564:2175110] 2th thread sleep 4 seconds
2018-12-01 20:54:57.575384+0800 dispatch_group-Demo[47564:2176028] 9th thread sleep 4 seconds
2018-12-01 20:54:58.575398+0800 dispatch_group-Demo[47564:2176022] 1th thread sleep 5 seconds
2018-12-01 20:54:59.577020+0800 dispatch_group-Demo[47564:2176024] 5th thread sleep 6 seconds
2018-12-01 20:55:01.575419+0800 dispatch_group-Demo[47564:2176023] 4th thread sleep 8 seconds
2018-12-01 20:55:02.578179+0800 dispatch_group-Demo[47564:2176015] 3th thread sleep 9 seconds
2018-12-01 20:55:02.578576+0800 dispatch_group-Demo[47564:2168009] all finished
dispatch_group_enter + dispatch_group_leave + dispatch_group_wait
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("jiang", DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(10, queue, ^(size_t n) {
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
int i = arc4random()%10;
sleep(i);
NSLog(@"%zuth thread sleep %d seconds",n,i);
dispatch_group_leave(group);
});
});
//dispatch_time_t timeout,表示等待时间。
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"all finished");
});
输出结果
2018-12-01 20:55:25.839995+0800 dispatch_group-Demo[47564:2176847] 6th thread sleep 0 seconds
2018-12-01 20:55:26.842480+0800 dispatch_group-Demo[47564:2176845] 1th thread sleep 1 seconds
2018-12-01 20:55:26.842484+0800 dispatch_group-Demo[47564:2176837] 5th thread sleep 1 seconds
2018-12-01 20:55:28.841831+0800 dispatch_group-Demo[47564:2176026] 3th thread sleep 3 seconds
2018-12-01 20:55:28.841859+0800 dispatch_group-Demo[47564:2176843] 2th thread sleep 3 seconds
2018-12-01 20:55:30.843902+0800 dispatch_group-Demo[47564:2176844] 0th thread sleep 5 seconds
2018-12-01 20:55:31.843272+0800 dispatch_group-Demo[47564:2176846] 4th thread sleep 6 seconds
2018-12-01 20:55:32.843084+0800 dispatch_group-Demo[47564:2176850] 9th thread sleep 7 seconds
2018-12-01 20:55:32.843088+0800 dispatch_group-Demo[47564:2176849] 8th thread sleep 7 seconds
2018-12-01 20:55:34.843344+0800 dispatch_group-Demo[47564:2176848] 7th thread sleep 9 seconds
2018-12-01 20:55:34.845123+0800 dispatch_group-Demo[47564:2168009] all finished
应用
我们经常不知道线程的queue是什么,dispatch_group_async(group, queue, block)很明显行不通。怎么办?
dispatch_group_enter(group) + dispatch_group_leave(group) + dispatch_group_notify(group,queue,block)
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
//假如这是一个异步的网络请求。
dispatch_async(dispatch_queue_create("queue1", DISPATCH_QUEUE_CONCURRENT), ^{
NSLog(@"start task1");
sleep(2);
NSLog(@"end task1");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(dispatch_queue_create("queue2", DISPATCH_QUEUE_CONCURRENT), ^{
NSLog(@"start task2");
sleep(3);
NSLog(@"end task2");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(dispatch_queue_create("queue3", DISPATCH_QUEUE_CONCURRENT), ^{
NSLog(@"start task3");
sleep(4);
NSLog(@"end task3");
dispatch_group_leave(group);
});
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_notify(group, queue, ^{
dispatch_async(queue, ^{
NSLog(@"start task4");
sleep(3);
NSLog(@"end task4");
});
dispatch_async(queue, ^{
NSLog(@"start task5");
sleep(3);
NSLog(@"end task5");
});
});
输出结果
2018-12-01 20:46:18.790292+0800 dispatch_group-Demo[47366:2161890] start task1
2018-12-01 20:46:18.790357+0800 dispatch_group-Demo[47366:2161891] start task3
2018-12-01 20:46:18.790382+0800 dispatch_group-Demo[47366:2161889] start task2
2018-12-01 20:46:20.791751+0800 dispatch_group-Demo[47366:2161890] end task1
2018-12-01 20:46:21.792203+0800 dispatch_group-Demo[47366:2161889] end task2
2018-12-01 20:46:22.791940+0800 dispatch_group-Demo[47366:2161891] end task3
2018-12-01 20:46:22.793406+0800 dispatch_group-Demo[47366:2161891] start task5
2018-12-01 20:46:22.793427+0800 dispatch_group-Demo[47366:2161889] start task4
2018-12-01 20:46:25.799664+0800 dispatch_group-Demo[47366:2161891] end task5
2018-12-01 20:46:25.799664+0800 dispatch_group-Demo[47366:2161889] end task4
总结
dispatch_group,经常用在执行多个异步并发的线程后,将这些线程的得到的数据,综合处理。
例如:多个网络请求后,数据统一解析;多个图片数据请求,多个图片合成一张图片。