需求是:C任务需要得到请求A,B的结果后再来执行.这种需求单独用组队列是不太好的.因为请求A,B都是异步的,完成需要有一定的耗时,而把任务放入队列组中,提交完AB两个任务后,就算完成了任务,并不会等AB两个异步请求返回请求结果.
- dispatch_semaphore GCD中的信号量,主要解决线程间的依赖.让多个线程之间相互协调完成任务,信号量会阻塞当前任务直到任务完成.
1.创建一个信号量,传入参数一般为0,不能传入小于0的值.
dispatch_semaphore_t dispatch_semaphore_create(long value);
2.使信号量等待,可以看做是信号量减一,后一个参数是等待时间.
long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);
3.放行信号量,可以看做是信号量加一,当信号量>=0的时候,程序就可以通过向下执行.
long dispatch_semaphore_signal(dispatch_semaphore_t dsema);
下面是DEMO,
1.模拟一个耗时的操作,通过传入不同大小的参数,并把任务放入子线程来模拟AB两个异步请求的不同耗时.
- (void)killTime:(long)num{
for (int i=0; i<num; i++) {
@autoreleasepool{
NSString * str = [NSString stringWithFormat:@"iodfi"];
}
}
}
2.信号量和队列组结合使用
- (void)cTask{
self.taskA = @"A没完成";
self.taskB = @"B没完成";
//1.创建信号量,组队列
NSLog(@"1-- %@",[NSThread currentThread]);//1
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_group_t group = dispatch_group_create();
//A请求
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"A group 1-- %@",[NSThread currentThread]);//3
//任务放入子线程模拟异步请求
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"A group 子线程-- %@",[NSThread currentThread]);//5
[self killTime:10e6];
self.taskA = @"A完成了";
dispatch_semaphore_signal(semaphore);
});
//让信号量等待
NSLog(@"A group 2-- %@",[NSThread currentThread]);//3
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
//B请求
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"B group 1 -- %@",[NSThread currentThread]);//4
//任务放入子线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"B group 子线程-- %@",[NSThread currentThread]);//6
[self killTime:10e3];
self.taskB = @"B完成了";
dispatch_semaphore_signal(semaphore);
});
//让信号量等待
NSLog(@"B group 2-- %@",[NSThread currentThread]);//4
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
//C任务
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"%@ --- %@",self.taskA,self.taskB);
});
}
打印的结果是
2018-03-19 19:01:46.843305+0800 005-信号量[1195:77040] 1-- <NSThread: 0x600000068a40>{number = 1, name = main}
2018-03-19 19:01:46.843634+0800 005-信号量[1195:77040] 主线程执行任务
2018-03-19 19:01:46.843755+0800 005-信号量[1195:77120] A group 1-- <NSThread: 0x600000267780>{number = 3, name = (null)}
2018-03-19 19:01:46.843796+0800 005-信号量[1195:77122] B group 1 -- <NSThread: 0x604000276500>{number = 4, name = (null)}
2018-03-19 19:01:46.845369+0800 005-信号量[1195:77120] A group 2-- <NSThread: 0x600000267780>{number = 3, name = (null)}
2018-03-19 19:01:46.845449+0800 005-信号量[1195:77121] A group 子线程-- <NSThread: 0x604000276900>{number = 5, name = (null)}
2018-03-19 19:01:46.846946+0800 005-信号量[1195:77122] B group 2-- <NSThread: 0x604000276500>{number = 4, name = (null)}
2018-03-19 19:01:46.847011+0800 005-信号量[1195:77123] B group 子线程-- <NSThread: 0x600000267c80>{number = 6, name = (null)}
2018-03-19 19:01:52.813326+0800 005-信号量[1195:77122] A完成了 --- B完成了
3.如何把和信号量相关的代码删除,可以看见结果是不正确的.
网友评论