多线程09——CGD队列组
一、需求说明
有这么1种需求
首先:分别异步执行2个耗时的操作
其次:等2个异步操作都执行完毕后,再回到主线程执行操作
如何解决??
当然我们可以使用 异步函数 + 并发队列 + 栅栏函数 来解决这问题。 除此之外,如果想要快速高效地实现上述需求,可以考虑用 队列组
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 执行1个耗时的异步操作
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 执行1个耗时的异步操作
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的异步操作都执行完毕后,回到主线程...
});
二、通知使用 dispatch_group_notify
2.1 队列组的使用可以分为四个步骤
1.创建队列
2.创建队列组
3.封装任务
4.拦截通知
2.2 示例代码
-(void)group1{
//1.创建队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//2.创建队列组
dispatch_group_t group = dispatch_group_create();
//3.封装任务
dispatch_group_async(group, queue, ^{
NSLog(@"1----------%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"2----------%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"3----------%@",[NSThread currentThread]);
});
//4.拦截通知
dispatch_group_notify(group, queue, ^{
NSLog(@"---dispatch_group_notify------%@",[NSThread currentThread]);
});
//不用等待 队列执行完就会执行这个代码
NSLog(@"----group1--end----");
}
2.2 结果
2017-09-03 04:09:35.086 03_UIview83多线程GCD[86781:501798] ----group1--end----
2017-09-03 04:09:35.086 03_UIview83多线程GCD[86781:502228] 1----------<NSThread: 0x60000007aa00>{number = 3, name = (null)}
2017-09-03 04:09:35.086 03_UIview83多线程GCD[86781:502234] 2----------<NSThread: 0x600000076fc0>{number = 4, name = (null)}
2017-09-03 04:09:35.086 03_UIview83多线程GCD[86781:502235] 3----------<NSThread: 0x608000262a80>{number = 5, name = (null)}
2017-09-03 04:09:35.086 03_UIview83多线程GCD[86781:502235] ---dispatch_group_notify------<NSThread: 0x608000262a80>{number = 5, name = (null)}
三、通知使用 dispatch_group_wait
3.1 示例代码
-(void)group2{
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"1----------%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"2----------%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"3----------%@",[NSThread currentThread]);
});
//等待,DISPATCH_TIME_FOREVER 知道队列中所有的任务执行完成了才能执行
dispatch_group_wait(group,DISPATCH_TIME_FOREVER);
//最后执行
NSLog(@"----group2--end----");
}
3.2 结果
2017-09-03 04:14:47.246 03_UIview83多线程GCD[87259:508074] 2----------<NSThread: 0x6000000718c0>{number = 4, name = (null)}
2017-09-03 04:14:47.246 03_UIview83多线程GCD[87259:508069] 1----------<NSThread: 0x600000071100>{number = 3, name = (null)}
2017-09-03 04:14:47.246 03_UIview83多线程GCD[87259:508075] 3----------<NSThread: 0x608000074000>{number = 5, name = (null)}
2017-09-03 04:14:47.247 03_UIview83多线程GCD[87259:504674] ----group2--end----
三、dispatch_group_enter 和 dispatch_group_leave 配套使用
4.1 说明
dispatch_group_enter
方法后面的异步任务会被纳入到队列组的监听范围,进入群组
dispatch_group_enter | dispatch_group_leave
必须要配对使用
“`
void
dispatch_group_enter(dispatch_group_t group);
void
dispatch_group_leave(dispatch_group_t group);
“`
4.2 示例代码
-(void)group3{
dispatch_queue_t queque = dispatch_get_global_queue(0, 0);
dispatch_group_t group = dispatch_group_create();
//在该方法后面的异步任务会被纳入到队列组的监听范围,进入群组
//dispatch_group_enter|dispatch_group_leave 必须要配对使用
dispatch_group_enter(group);
dispatch_async(queque, ^{
NSLog(@"1----------%@",[NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queque, ^{
NSLog(@"2----------%@",[NSThread currentThread]);
dispatch_group_leave(group);
});
//等待,DISPATCH_TIME_FOREVER 知道队列中所有的任务执行完成了才能执行
dispatch_group_wait(group,DISPATCH_TIME_FOREVER);
NSLog(@"----group3--end----");
}
4.3 结果
2017-09-03 04:15:42.832 03_UIview83多线程GCD[87781:509477] 1----------<NSThread: 0x600000269f00>{number = 3, name = (null)}
2017-09-03 04:15:42.832 03_UIview83多线程GCD[87781:509482] 2----------<NSThread: 0x600000269f40>{number = 4, name = (null)}
2017-09-03 04:15:42.832 03_UIview83多线程GCD[87781:508866] ----group3--end----