忘了for循環(huán)吼畏,忘了艱澀難懂的信號(hào)量督赤。多了不說上代碼,不懂自己腦補(bǔ)宫仗。
@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(@"測(cè)試方法執(zhí)行完畢");
});
}
- (void)asynTestFuction1:(dispatch_group_t)group
{
dispatch_async(self.queue, ^{
for (NSInteger i = 0;i < 10;i++) {
NSInteger index = i;
}
NSLog(@"執(zhí)行方法一");
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(@"執(zhí)行方法二");
dispatch_group_leave(group);
});
}
@end
執(zhí)行結(jié)果如下:
2017-08-26 20:51:30.423169+0800 GCDGroupTest[5009:494839] 執(zhí)行方法一
2017-08-26 20:51:30.423171+0800 GCDGroupTest[5009:494837] 執(zhí)行方法二
2017-08-26 20:51:30.423378+0800 GCDGroupTest[5009:494839] 測(cè)試方法執(zhí)行完畢
可加群一起交流共同學(xué)習(xí):801216530够挂。