忘了for循环,忘了艰涩难懂的信号量。多了不说上代码,不懂自己脑补。
@interface ViewController ()
@property (nonatomic, strong) dispatch_queue_t queue;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.queue = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t groupQueue = dispatch_queue_create(0, 0);
__weak typeof(self) weakSelf = self;
dispatch_group_async(group, groupQueue, ^{
dispatch_group_enter(group);
[weakSelf asynTestFunction2:group];
});
dispatch_group_async(group, groupQueue, ^{
dispatch_group_enter(group);
[weakSelf asynTestFuction1:group];
});
dispatch_group_notify(group, groupQueue, ^{
NSLog(@"测试方法执行完毕");
});
}
- (void)asynTestFuction1:(dispatch_group_t)group
{
dispatch_async(self.queue, ^{
for (NSInteger i = 0;i < 10;i++) {
NSInteger index = i;
}
NSLog(@"执行方法一");
dispatch_group_leave(group);
});
}
- (void)asynTestFunction2:(dispatch_group_t)group
{
dispatch_async(self.queue, ^{
for (NSInteger i = 0;i < 10000;i++) {
NSInteger index = i;
}
NSLog(@"执行方法二");
dispatch_group_leave(group);
});
}
@end
执行结果如下:
2017-08-26 20:51:30.423169+0800 GCDGroupTest[5009:494839] 执行方法一
2017-08-26 20:51:30.423171+0800 GCDGroupTest[5009:494837] 执行方法二
2017-08-26 20:51:30.423378+0800 GCDGroupTest[5009:494839] 测试方法执行完毕
可加群一起交流共同学习:801216530。