dispatch_group_enter和dispatch_group_leave必须成对出现 ,来给group添加新任务。
dispatch_group_enter(group)
//执行的事件
dispatch_group_leave(group)
为什么会用到这个方法呢?
因为在dispatch_group_async()里使用dispatch_async()方法,dispatch_group_notify不是在最后执行的。而dispatch_sync()方法调用的时候正常。代码如下:
一、使用dispatch_group_async()方法
1、异步里异步并发
dispatch_queue_t queue = dispatch_queue_create("dispatchGroupMethod1.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
dispatch_async(queue, ^{
for (NSInteger i =0; i<3; i++) {
sleep(1);
NSLog(@"任务1-异步任务执行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
}
});
});
dispatch_group_async(group, queue, ^{
dispatch_async(queue, ^{
for (NSInteger i =0; i<3; i++) {
sleep(1);
NSLog(@"任务2-异步任务执行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
}
});
});
//等待上面的任务全部完成后,会收到通知执行block中的代码 (不会阻塞线程)
dispatch_group_notify(group, queue, ^{
NSLog(@"全部任务执行完成");
});
//打印如下
2018-03-09 16:54:37.009913+0800 ceshi[3934:349413] 全部任务执行完成
2018-03-09 16:54:38.014204+0800 ceshi[3934:349411] 任务1-异步任务执行-:0,thread:<NSThread: 0x60000026cb00>{number = 3, name = (null)}
2018-03-09 16:54:38.014204+0800 ceshi[3934:349414] 任务2-异步任务执行-:0,thread:<NSThread: 0x6040004799c0>{number = 4, name = (null)}
2018-03-09 16:54:39.018626+0800 ceshi[3934:349414] 任务2-异步任务执行-:1,thread:<NSThread: 0x6040004799c0>{number = 4, name = (null)}
2018-03-09 16:54:39.018626+0800 ceshi[3934:349411] 任务1-异步任务执行-:1,thread:<NSThread: 0x60000026cb00>{number = 3, name = (null)}
2018-03-09 16:54:40.020653+0800 ceshi[3934:349411] 任务1-异步任务执行-:2,thread:<NSThread: 0x60000026cb00>{number = 3, name = (null)}
2018-03-09 16:54:40.020653+0800 ceshi[3934:349414] 任务2-异步任务执行-:2,thread:<NSThread: 0x6040004799c0>{number = 4, name = (null)}
异步并发,dispatch_group_notify先执行,没在最后调用。
2、异步里同步并发:
dispatch_queue_t queue = dispatch_queue_create("dispatchGroupMethod1.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
dispatch_sync(queue, ^{
for (NSInteger i =0; i<3; i++) {
sleep(1);
NSLog(@"任务1-同步任务执行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
}
});
});
dispatch_group_async(group, queue, ^{
dispatch_sync(queue, ^{
for (NSInteger i =0; i<3; i++) {
sleep(1);
NSLog(@"任务2-同步任务执行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
}
});
});
//等待上面的任务全部完成后,会收到通知执行block中的代码 (不会阻塞线程)
dispatch_group_notify(group, queue, ^{
NSLog(@"全部任务执行完成");
});
//打印如下
2018-03-09 16:56:42.372297+0800 ceshi[3985:353717] 任务2-同步任务执行-:0,thread:<NSThread: 0x604000273e40>{number = 4, name = (null)}
2018-03-09 16:56:42.372297+0800 ceshi[3985:353715] 任务1-同步任务执行-:0,thread:<NSThread: 0x604000273e00>{number = 3, name = (null)}
2018-03-09 16:56:43.374677+0800 ceshi[3985:353717] 任务2-同步任务执行-:1,thread:<NSThread: 0x604000273e40>{number = 4, name = (null)}
2018-03-09 16:56:43.374677+0800 ceshi[3985:353715] 任务1-同步任务执行-:1,thread:<NSThread: 0x604000273e00>{number = 3, name = (null)}
2018-03-09 16:56:44.380101+0800 ceshi[3985:353715] 任务1-同步任务执行-:2,thread:<NSThread: 0x604000273e00>{number = 3, name = (null)}
2018-03-09 16:56:44.380101+0800 ceshi[3985:353717] 任务2-同步任务执行-:2,thread:<NSThread: 0x604000273e40>{number = 4, name = (null)}
2018-03-09 16:56:44.380617+0800 ceshi[3985:353717] 全部任务执行完成
二、使用dispatch_group_enter和dispatch_group_leave
1、异步执行
//异步
dispatch_queue_t queue = dispatch_queue_create("dispatchGroupMethod1.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_async(queue, ^{
dispatch_group_enter(group);
for (NSInteger i =0; i<3; i++) {
sleep(1);
NSLog(@"任务1-异步任务执行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
}
dispatch_group_leave(group);
});
dispatch_async(queue, ^{
dispatch_group_enter(group);
for (NSInteger i =0; i<3; i++) {
sleep(1);
NSLog(@"任务2-异步任务执行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
}
dispatch_group_leave(group);
});
//等待上面的任务全部完成后,会收到通知执行block中的代码 (不会阻塞线程)
dispatch_group_notify(group, queue, ^{
NSLog(@"全部任务执行完成");
});
//结果
2018-03-09 17:28:47.644739+0800 ceshi[4322:392667] 任务2-异步任务执行-:0,thread:<NSThread: 0x600000266b00>{number = 4, name = (null)}
2018-03-09 17:28:47.644739+0800 ceshi[4322:392666] 任务1-异步任务执行-:0,thread:<NSThread: 0x60400027dbc0>{number = 3, name = (null)}
2018-03-09 17:28:48.646245+0800 ceshi[4322:392667] 任务2-异步任务执行-:1,thread:<NSThread: 0x600000266b00>{number = 4, name = (null)}
2018-03-09 17:28:48.646245+0800 ceshi[4322:392666] 任务1-异步任务执行-:1,thread:<NSThread: 0x60400027dbc0>{number = 3, name = (null)}
2018-03-09 17:28:49.650996+0800 ceshi[4322:392667] 任务2-异步任务执行-:2,thread:<NSThread: 0x600000266b00>{number = 4, name = (null)}
2018-03-09 17:28:49.650996+0800 ceshi[4322:392666] 任务1-异步任务执行-:2,thread:<NSThread: 0x60400027dbc0>{number = 3, name = (null)}
2018-03-09 17:28:49.651347+0800 ceshi[4322:392666] 全部任务执行完成
2、同步执行
//同步
dispatch_queue_t queue = dispatch_queue_create("dispatchGroupMethod1.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_sync(queue, ^{
dispatch_group_enter(group);
for (NSInteger i =0; i<3; i++) {
sleep(1);
NSLog(@"任务1-同步任务执行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
}
dispatch_group_leave(group);
});
dispatch_sync(queue, ^{
dispatch_group_enter(group);
for (NSInteger i =0; i<3; i++) {
sleep(1);
NSLog(@"任务2-同步任务执行-:%ld,thread:%@",(long)i,[NSThread currentThread]);
}
dispatch_group_leave(group);
});
//等待上面的任务全部完成后,会收到通知执行block中的代码 (不会阻塞线程)
dispatch_group_notify(group, queue, ^{
NSLog(@"全部任务执行完成");
});
//结果
2018-03-09 17:42:34.271283+0800 ceshi[4454:413740] 任务1-同步任务执行-:0,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:35.272241+0800 ceshi[4454:413740] 任务1-同步任务执行-:1,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:36.273663+0800 ceshi[4454:413740] 任务1-同步任务执行-:2,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:37.275635+0800 ceshi[4454:413740] 任务2-同步任务执行-:0,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:38.276095+0800 ceshi[4454:413740] 任务2-同步任务执行-:1,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:39.277732+0800 ceshi[4454:413740] 任务2-同步任务执行-:2,thread:<NSThread: 0x600000068980>{number = 1, name = main}
2018-03-09 17:42:39.278429+0800 ceshi[4454:413852] 全部任务执行完成
网友评论