封装任务并把任务添加到队列中
参数1: 调度组
参数2: 队列
参数3: 封装任务
notify和wait都可以 就是wait会阻塞线程 notify 不会阻塞
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
NSLog(@"--1");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
NSLog(@"--2");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
NSLog(@"--3");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
NSLog(@"--4");
dispatch_group_leave(group);
});
/* 当队列组中所有的任务都执行完毕的时候调用下面的函数 函数本身是不阻塞的 */
// dispatch_group_notify(group, queue, ^{
// NSLog(@"完了");
// });
/*函数本身是阻塞的*/
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"执行完毕了是吗");
// 必须等到1,2,3,4三个任务完成了以后才会执行dispatch_group_notify(,,_)中block代码块中的代码.