NSBlockOperation和GCD线程组的组合实现多线程下
//全局的NSOperationQueue
- (NSOperationQueue *)queue {
if(!_queue){
_queue = [NSOperationQueue new];
//设置最大并发数为3
_queue.maxConcurrentOperationCount = 3;
}
return _queue;
}
//开始下载任务
- (void)beginDownloadTask {
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
NSLog(@"进入组任务");
[self asyncDownloadCompleted:^{
dispatch_group_leave(group);
NSLog(@"组任务执行完毕");
}];
//dispatch_group_wait,同步的方法,会一直等待group中的任务执行完毕才会执行后面的东西
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"NSBlockOperation执行完毕");
}];
[self.queue addOperation:operation];
}
//模拟一个异步下载
- (void)asyncDownloadCompleted:(void(^)(void))completedBlock {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"下载中,当前线程:%@",[NSThread currentThread]);
//模拟网络延时
[NSThread sleepForTimeInterval:5];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"下载完成");
if(completedBlock){
completedBlock();
}
});
});
}
//模拟开始10个下载任务
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
for(int i=0;i<10;i++){
[self beginDownloadTask];
}
}
打印的日志如下:
2018-01-12 10:50:56.173984+0800 MultiDownload[1018:108225] 进入组任务
2018-01-12 10:50:56.173983+0800 MultiDownload[1018:107387] 进入组任务
2018-01-12 10:50:56.174022+0800 MultiDownload[1018:108223] 进入组任务
2018-01-12 10:50:56.174338+0800 MultiDownload[1018:108226] 下载中,当前线程:<NSThread: 0x604000462400>{number = 3, name = (null)}
2018-01-12 10:50:56.174346+0800 MultiDownload[1018:108227] 下载中,当前线程:<NSThread: 0x600000077980>{number = 4, name = (null)}
2018-01-12 10:50:56.174571+0800 MultiDownload[1018:108228] 下载中,当前线程:<NSThread: 0x604000462280>{number = 5, name = (null)}
2018-01-12 10:51:01.180133+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:01.180500+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:01.180513+0800 MultiDownload[1018:107387] NSBlockOperation执行完毕
2018-01-12 10:51:01.180795+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:01.181042+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:01.181065+0800 MultiDownload[1018:107387] 进入组任务
2018-01-12 10:51:01.181080+0800 MultiDownload[1018:108225] NSBlockOperation执行完毕
2018-01-12 10:51:01.181298+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:01.181718+0800 MultiDownload[1018:108225] 进入组任务
2018-01-12 10:51:01.181718+0800 MultiDownload[1018:108226] 下载中,当前线程:<NSThread: 0x604000462400>{number = 3, name = (null)}
2018-01-12 10:51:01.182009+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:01.182022+0800 MultiDownload[1018:108223] NSBlockOperation执行完毕
2018-01-12 10:51:01.182596+0800 MultiDownload[1018:108228] 下载中,当前线程:<NSThread: 0x604000462280>{number = 5, name = (null)}
2018-01-12 10:51:01.183821+0800 MultiDownload[1018:108227] 进入组任务
2018-01-12 10:51:01.184539+0800 MultiDownload[1018:108223] 下载中,当前线程:<NSThread: 0x604000464540>{number = 6, name = (null)}
2018-01-12 10:51:06.186875+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:06.187142+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:06.187170+0800 MultiDownload[1018:107387] NSBlockOperation执行完毕
2018-01-12 10:51:06.187384+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:06.187513+0800 MultiDownload[1018:108228] 进入组任务
2018-01-12 10:51:06.187564+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:06.187573+0800 MultiDownload[1018:108227] NSBlockOperation执行完毕
2018-01-12 10:51:06.187795+0800 MultiDownload[1018:107387] 下载中,当前线程:<NSThread: 0x60000007f280>{number = 7, name = (null)}
2018-01-12 10:51:06.187822+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:06.188008+0800 MultiDownload[1018:108226] 进入组任务
2018-01-12 10:51:06.188820+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:06.188824+0800 MultiDownload[1018:108225] NSBlockOperation执行完毕
2018-01-12 10:51:06.189172+0800 MultiDownload[1018:108227] 下载中,当前线程:<NSThread: 0x600000077980>{number = 4, name = (null)}
2018-01-12 10:51:06.190416+0800 MultiDownload[1018:108223] 进入组任务
2018-01-12 10:51:06.190718+0800 MultiDownload[1018:108225] 下载中,当前线程:<NSThread: 0x604000464480>{number = 8, name = (null)}
2018-01-12 10:51:11.189808+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:11.190120+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:11.190153+0800 MultiDownload[1018:108228] NSBlockOperation执行完毕
2018-01-12 10:51:11.190584+0800 MultiDownload[1018:107387] 进入组任务
2018-01-12 10:51:11.190732+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:11.191016+0800 MultiDownload[1018:108227] 下载中,当前线程:<NSThread: 0x600000077980>{number = 4, name = (null)}
2018-01-12 10:51:11.191159+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:11.191178+0800 MultiDownload[1018:108226] NSBlockOperation执行完毕
2018-01-12 10:51:11.191628+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:11.191973+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:11.191990+0800 MultiDownload[1018:108223] NSBlockOperation执行完毕
2018-01-12 10:51:16.196486+0800 MultiDownload[1018:107164] 下载完成
2018-01-12 10:51:16.196807+0800 MultiDownload[1018:107164] 组任务执行完毕
2018-01-12 10:51:16.196818+0800 MultiDownload[1018:107387] NSBlockOperation执行完毕
//从打印数据上我们可以看出10个并行下载的任务,同时进行的只有3个,前面的任务执行完毕之后,后面的任务才会接着执行.