延时执行三种方法 并行/串行/主队列综合练习 队列组
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[selfdemo];
}
//延时执行
-(void)demo{
//方法1 timer
// NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(task) userInfo:nil repeats:YES];
// [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
//方法2
[selfperformSelector:@selector(task)withObject:nilafterDelay:3];
//方法3
/*参数1:延时时间 dispatch_time生成时间 纳秒为计时单位 精度高
*参数2:队列
*参数3:任务
*异步执行 */
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
NSLog(@"task");
});
NSLog(@"over");
}
-(void)task{
NSLog(@"task");
}
//模拟下载任务
/*1、下载 L01.zip
*2、下载 L02.zip
*3、通知UI:下载完成
1、2在3之前执行
*/
-(void)demo1{
//串行,异步顺序下载
dispatch_queue_t serialQueue =dispatch_queue_create("serialQueue",DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
[NSThreadsleepForTimeInterval:arc4random_uniform(5)];//5秒内的随机时间
dispatch_async(serialQueue, ^{
NSLog(@"%@下载 L01.zip",[NSThreadcurrentThread]);
});
[NSThreadsleepForTimeInterval:arc4random_uniform(5)];//5秒内的随机时间
dispatch_async(serialQueue, ^{
NSLog(@"%@下载 L02.zip",[NSThreadcurrentThread]);
});
dispatch_async(serialQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@下载完成",[NSThreadcurrentThread]);
});
});
});
}
-(void)demo2{
//并行异步 "shift+command+o"快速查找
dispatch_queue_t concurrentQueue =dispatch_queue_create("concurrentQueue",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
[NSThreadsleepForTimeInterval:arc4random_uniform(5)];//5秒内的随机时间
//并行同步顺序下载
dispatch_sync(concurrentQueue, ^{
NSLog(@"%@下载 L01.zip",[NSThreadcurrentThread]);
});
[NSThreadsleepForTimeInterval:arc4random_uniform(5)];//5秒内的随机时间
dispatch_sync(concurrentQueue, ^{
NSLog(@"%@下载 L02.zip",[NSThreadcurrentThread]);
});
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@下载完成",[NSThreadcurrentThread]);
});
});
}
//任务1、2在子线程上先顺序执行后,任务3、4在主线程上执行,最后任务5、6、7在子线程上并发无序执行
-(void)demo3{
dispatch_queue_t serialQueue =dispatch_queue_create("serialQueue",DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue =dispatch_queue_create("concurrentQueue",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
dispatch_sync(serialQueue, ^{
NSLog(@"%@执行任务1",[NSThreadcurrentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"%@执行任务2",[NSThreadcurrentThread]);
});
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@执行任务3",[NSThreadcurrentThread]);
});
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@执行任务4",[NSThreadcurrentThread]);
});
dispatch_async(concurrentQueue, ^{
dispatch_async(concurrentQueue, ^{
NSLog(@"%@执行任务5",[NSThreadcurrentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"%@执行任务6",[NSThreadcurrentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"%@执行任务7",[NSThreadcurrentThread]);
});
});
});
}
//队列组
-(void)demo4{
NSLog(@"begin");
//创建队列组
dispatch_group_t group =dispatch_group_create();
//开启异步任务
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
//模拟网络卡
[NSThreadsleepForTimeInterval:arc4random_uniform(5)];//休眠5秒内随机时间
NSLog(@"%@下载 L01.zip",[NSThreadcurrentThread]);
});
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
//模拟网络卡
[NSThreadsleepForTimeInterval:arc4random_uniform(5)];//休眠5秒内随机时间
NSLog(@"%@下载 L02.zip",[NSThreadcurrentThread]);
});
dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{
NSLog(@"%@下载完成",[NSThreadcurrentThread]);
});
}
-(void)demo5{
/* dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block)
{
dispatch_retain(group);
dispatch_group_enter(group);
dispatch_async(queue, ^{
block();
dispatch_group_leave(group);
dispatch_release(group);
}); */
dispatch_group_t group =dispatch_group_create();
dispatch_group_enter(group);
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
//模拟网络卡
[NSThreadsleepForTimeInterval:arc4random_uniform(5)];//休眠5秒内随机时间
NSLog(@"%@下载 L01.zip",[NSThreadcurrentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
//模拟网络卡
[NSThreadsleepForTimeInterval:arc4random_uniform(5)];//休眠5秒内随机时间
NSLog(@"%@下载 L02.zip",[NSThreadcurrentThread]);
dispatch_group_leave(group);
});
dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{
NSLog(@"%@下载完成",[NSThreadcurrentThread]);
});
}
两个网络请求同步问题
- (void)loadData {
NSString *appIdKey = @"8781e4ef1c73ff20a180d3d7a42a8c04";
NSString* urlString_1 = @"http://api.openweathermap.org/data/2.5/weather";
NSString* urlString_2 = @"http://api.openweathermap.org/data/2.5/forecast/daily";
NSDictionary* dictionary =@{@"lat":@"40.04991291",
@"lon":@"116.25626162",
@"APPID" : appIdKey};
// 创建组
dispatch_group_t group = dispatch_group_create();
// 将第一个网络请求任务添加组中
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 创建信号量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// 开始网络请求任务
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:urlString_1 parameters:dictionary progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"成功请求数据1:%@",[responseObject class]);
// 如果请求成功,发送信号量
dispatch_semaphore_signal(semaphore);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失败请求数据");
// 如果请求失败,也发送信号量
dispatch_semaphore_signal(semaphore);
}];
// 在网络请求任务成功之前,信号量等待中
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
// 将第二个网络请求任务添加到组中
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 创建信号量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// 开始网络请求任务
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:urlString_2
parameters:dictionary
progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"成功请求数据2:%@",[responseObject class]);
// 如果请求成功,发送信号量
dispatch_semaphore_signal(semaphore);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失败请求数据");
// 如果请求失败,也发送信号量
dispatch_semaphore_signal(semaphore);
}];
// 在网络请求任务成功之前,信号量等待中
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"完成了网络请求,不管网络请求失败了还是成功了。");
});
}