我们常用的GCD调度组方式

 //GCD常用调度组写法
-(void)demo1{ //创建调度组和队列
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("ChangYong", DISPATCH_QUEUE_CONCURRENT); //将任务添加到调度组
dispatch_group_async(group, queue, ^{
NSLog(@"耗时操作_11111111");
});
dispatch_group_async(group, queue, ^{
//模拟耗时操作
[NSThread sleepForTimeInterval:];
NSLog(@"耗时操作_22222222");
});
dispatch_group_async(group, queue, ^{
NSLog(@"耗时操作_33333333");
}); //所有任务执行完毕后执行的任务
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"所有任务执行完毕, 更新UI操作");
});
}

GCD调度组原理

 //GCD调度组原理方法
-(void)demo2{ //创建调度组和队列
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("YuanLi", DISPATCH_QUEUE_CONCURRENT); //任务1
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"任务1");
dispatch_group_leave(group);
}); //任务2
dispatch_group_enter(group);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:];
NSLog(@"任务2");
dispatch_group_leave(group);
}); //任务3
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"任务3");
dispatch_group_leave(group);
}); //任务全部执行完毕
dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"任务执行完毕, 刷新UI");
}); //等待上面代码执行完毕后在执行下面的代码, 参数2:等待超时时间
//dispatch_group_wait(group, DISPATCH_TIME_FOREVER); NSLog(@"Hello");
}
05-17 18:36