本篇仅仅是异步任务顺序执行探索的代码记录,大家有需要的可以拿去测试:
/** queue */
@property(nonatomic,strong) dispatch_queue_t myqueue ;
/** mask */
@property(nonatomic,strong) NSMutableArray *mask;
- (void)viewDidLoad {
[super viewDidLoad];
_myqueue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
[self sendBatchRequest2:@""];
NSLog(@"哈哈哈哈");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"我试下主线程卡顿了吗");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"我试下主线程卡顿了吗2");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"我试下主线程卡顿了吗2");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"我试下主线程卡顿了吗2");
});
}
#pragma mark - 利用信号量来实现异步顺序执行
#pragma mark -
- (void)sendBatchRequest2:(NSString *)str {
dispatch_semaphore_t sema = dispatch_semaphore_create(1);
for (int i = 0 ; i < self.mask.count; i++) {
str = self.mask[i];
dispatch_async(_myqueue, ^{
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
[NSThread sleepForTimeInterval:2];
NSLog(@"这是第%d个任务============字符串为:%@=====线程为%@",i,str,[NSThread currentThread]);
dispatch_semaphore_signal(sema);
});
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mask addObject:@"我是临时加入的"];
NSLog(@"临时加入了元素到数组中");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mask addObject:@"我是临时加入的"];
NSLog(@"临时加入了元素到数组中");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mask addObject:@"我是临时加入的"];
NSLog(@"临时加入了元素到数组中");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mask addObject:@"我是临时加入的"];
NSLog(@"临时加入了元素到数组中");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mask addObject:@"我是临时加入的"];
NSLog(@"临时加入了元素到数组中");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mask addObject:@"我是临时加入的"];
NSLog(@"临时加入了元素到数组中");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mask addObject:@"我是临时加入的"];
NSLog(@"临时加入了元素到数组中");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.mask addObject:@"我是临时加入的"];
NSLog(@"临时加入了元素到数组中");
});
NSLog(@"44444");
}
- (NSMutableArray *)mask {
if (!_mask) {
_mask = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
[_mask addObject:[[NSString alloc]initWithFormat:@"%d",i] ];
}
}
return _mask;
}
这里的myQueue是串行队列,如果换成全局的glob会出问题。
补充:
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_group_async(_group, _myqueue, ^{
NSDictionary *parameters = @{
@"key":@"appKey",
@"info":userSay,
@"userid" : @"abc123"
};
NSString *urlString = @"http://www.tuling123.com/openapi/api";
[[NTManager shareManager] POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:NULL];
NSLog(@"%@",jsonObject);
dispatch_semaphore_signal(sema);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
});