GCD 实现多个异步的同步执行-dispatch_group_t
2017-07-26 本文已影响172人
蝼蚁撼树
一. dispatch_group_t 用法
1. dispatch_group_t 执行同步操作
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t globeQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_async(group, globeQ, ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"1--over");
});
dispatch_group_async(group, globeQ, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"2--over");
});
dispatch_group_async(group, globeQ, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"3--over");
});
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"全部结束......");
});
NSLog(@" waiting... ");
运行,打印结果如下
2017-07-26 15:10:07.166248 测试用test[15173:289444] waiting...
2017-07-26 15:10:09.170468 测试用test[15173:289523] 3--over
2017-07-26 15:10:10.167656 测试用test[15173:289495] 1--over
2017-07-26 15:10:12.170615 测试用test[15173:289497] 2--over
2017-07-26 15:10:12.170749 测试用test[15173:289497] 全部结束......
从上面的步骤可以得出
I.异步操作,异步
并且 无序执行
;
II.三个dispatch_group
执行全部完成之后,会执行 dispatch_group_notify
里面的block
III.waiting...
立即执行,dispatch_group_t
不会阻塞当前线程;
但是,当dispatch_group_async
的block
里面执行的是异步任务,如果还是使用上面的方法你会发现异步任务还没跑完就已经进入到了dispatch_group_notify
里面的block
.例如下面:
dispatch_group_t group = dispatch_group_create();
// dispatch_queue_t globeQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t concurrentQ = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, concurrentQ, ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"1--over");
});
});
dispatch_group_async(group, concurrentQ, ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"2--over");
});
});
dispatch_group_async(group, concurrentQ, ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"3--over");
});
});
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"全部结束......");
});
NSLog(@" waiting... ");
运行,打印结果如下
2017-07-26 15:23:14.959950 测试用test[15550:300115] waiting...
2017-07-26 15:23:14.960001 测试用test[15550:300152] 全部结束......
2017-07-26 15:23:16.962408 测试用test[15550:300168] 3--over
2017-07-26 15:23:17.960724 测试用test[15550:300154] 1--over
2017-07-26 15:23:19.962464 测试用test[15550:300164] 2--over
从上面的结果可看出, 当执行是异步操作的 使用上面的方法无法满足要求.这时用到dispatch_group_enter
和dispatch_group_leave
就可以解决这个问题:
2. dispatch_group_t 执行的是异步操作
示例
dispatch_group_t group = dispatch_group_create();
// dispatch_queue_t globeQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t concurrentQ = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_enter(group);
dispatch_async(concurrentQ, ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"1--over");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_async(concurrentQ, ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"2--over");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);//可以写在每个组前面,也可以全部提到任务最前面
dispatch_async(concurrentQ, ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"3--over");
dispatch_group_leave(group);
});
});
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"全部结束......");
});
NSLog(@" waiting... ");
创建好任务组后,执行加入任务组的操作代码。dispatch_enter
和dispatch_leave
要成对出现,否则奔溃。
运行,打印如下:
2017-07-26 15:35:09.359028 测试用test[15895:310263] waiting...
2017-07-26 15:35:11.362995 测试用test[15895:310296] 3--over
2017-07-26 15:35:12.361839 测试用test[15895:310297] 1--over
2017-07-26 15:35:14.364044 测试用test[15895:310321] 2--over
2017-07-26 15:35:14.364166 测试用test[15895:310321] 全部结束......
满足需求,OK!!
二. dispatch_semaphore_t 用法
示例:
执行的是同步操作
for (int i = 0; i<3; ++i) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_queue_t globeQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(globeQ, ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"%d--over",i+1);
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"%d--through",i+1);
}
NSLog(@" waiting... ");
执行的是异步操作
for (int i = 0; i<3; ++i) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// dispatch_queue_t globeQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t concurrentQ = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQ, ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"%d--over",i+1);
dispatch_semaphore_signal(semaphore);
});
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"%d--through",i+1);
}
NSLog(@" waiting... ");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
; 如果semaphore
计数大于等于1.计数-1,返回,程序继续运行。如果计数为0,则等待。这里设置的等待时间是一直等待。dispatch_semaphore_signal(semaphore)
;计数+1.在这两句代码中间的执行代码,每次只会允许一个线程进入,这样就有效的保证了在多线程环境下,只能有一个线程进入。
运行,打印结果全部如下
2017-07-26 13:57:20.748453 测试用test[9697:223538] 1--over
2017-07-26 13:57:20.748523 测试用test[9697:223493] 1--through
2017-07-26 13:57:23.750806 测试用test[9697:223538] 2--over
2017-07-26 13:57:23.750881 测试用test[9697:223493] 2--through
2017-07-26 13:57:26.754020 测试用test[9697:223538] 3--over
2017-07-26 13:57:26.754095 测试用test[9697:223493] 3--through
2017-07-26 13:57:26.754117 测试用test[9697:223493] waiting...
从上面的步骤可以得出
I.异步操作,同步 并且 顺序执行;
II.waiting...
最后执行,dispatch_semaphore_t
信号量会阻塞当前进程;
综合对比,两种方式都可以满足 实现异步同步执行的需求,具体选择 就要看个人了...