问题引出
最近在看GCD多线程方面的东西,偶然看到一个问题如下:有A B C D E五个任务,D需要 A 和B 都执行完了才执行,E需要B 和C执行完才开始执行,给出一个GCD的实现方案
解决方案
- 第一反应是用dispatch_group,等A B C 都执行完再执行 D E,这种解决方案如果C消耗的时间比较长,会阻塞任务D,代码如下
dispatch_group_t groupTask = dispatch_group_create();
dispatch_queue_t groupQueue = dispatch_queue_create("com.task.group", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(groupTask, groupQueue, ^{
NSLog(@"Doing task A");
sleep(5);
NSLog(@"A complete");
});
dispatch_group_async(groupTask, groupQueue, ^{
NSLog(@"Doing task B");
sleep(5);
NSLog(@"B complete");
});
dispatch_group_async(groupTask, groupQueue, ^{
NSLog(@"Doing task C");
sleep(5);
NSLog(@"C complete");
});
dispatch_group_notify(groupTask, groupQueue, ^{
NSLog(@"Doing task D");
NSLog(@"Doing task E");
});
执行结果:
2019-07-15 17:25:11.299511+0800 TestBlock[95871:4803740] Doing task A
2019-07-15 17:25:11.299513+0800 TestBlock[95871:4803742] Doing task B
2019-07-15 17:25:11.299517+0800 TestBlock[95871:4803739] Doing task C
2019-07-15 17:25:16.303104+0800 TestBlock[95871:4803740] A complete
2019-07-15 17:25:16.303104+0800 TestBlock[95871:4803739] C complete
2019-07-15 17:25:16.303104+0800 TestBlock[95871:4803742] B complete
2019-07-15 17:25:16.303581+0800 TestBlock[95871:4803742] Doing task D
2019-07-15 17:25:16.303714+0800 TestBlock[95871:4803742] Doing task E
- 这个方案好像不太符合需求,再思考一遍题目,D 对A和B有依赖,这个用dispatch_group 来解决,E 对B 和 C有依赖,E在执行时,需要B的执行状态,由此联想到可以用信号量控制,因此引出以下的解决方案,
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t groupQueue = dispatch_queue_create("com.group.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
// run task A B in group
dispatch_group_async(group, groupQueue, ^{
NSLog(@"Doing task A");
sleep(5);
NSLog(@"A complete");
});
dispatch_group_async(group, groupQueue, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"Doing task B");
sleep(10);
NSLog(@"B complete");
dispatch_semaphore_signal(semaphore);
});
// run task C in another group
dispatch_group_t group2 = dispatch_group_create();
dispatch_group_async(group2, groupQueue, ^{
NSLog(@"Doing task C");
sleep(5);
NSLog(@"C complete");
});
// After A and B completed, run task D
dispatch_group_notify(group, groupQueue, ^{
NSLog(@"Doing task D");
});
// After B and C completed, run task E
dispatch_group_notify(group2, groupQueue, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"Doing task E");
dispatch_semaphore_signal(semaphore);
});
C执行完要执行E时,查看B任务的状态,B完成了就可以开始,执行结果如下:
2019-07-15 17:37:31.511863+0800 TestBlock[96007:4818708] Doing task A
2019-07-15 17:37:31.511867+0800 TestBlock[96007:4818707] Doing task C
2019-07-15 17:37:31.511863+0800 TestBlock[96007:4818710] Doing task B
2019-07-15 17:37:36.515497+0800 TestBlock[96007:4818708] A complete
2019-07-15 17:37:41.513354+0800 TestBlock[96007:4818710] B complete
2019-07-15 17:37:41.513640+0800 TestBlock[96007:4818710] Doing task D
2019-07-15 17:37:46.513255+0800 TestBlock[96007:4818707] C complete
2019-07-15 17:37:46.513455+0800 TestBlock[96007:4818707] Doing task E
由以上的执行结果可以看出,C如果是比较耗时的任务,不会阻塞任务D
总结
如果用GCD实现一些依赖性的任务,可以考虑GCD + semaphore 结合的方式。另外,如有更好的解决方案,可以告知。
网友评论