先前,项目中, 当时出现一些摸不着头脑的崩溃, 初步分析是 线程管理没限制好 导致的.
并且当时记录有Demo, 一直还未分享, 正好今夜打完🏀 精力旺盛有time ,挑灯夜笔挥一把~
bugly 问题现象如下
特此, 把各种线程管理方式 收集 罗列一下
< 0 >. GCD没有像NSOperation提供MaxConcurrentOperationCount
方法, 所以一般我们在偶尔一次异步时这么用
// GCD, dispatch_async
- (void)test_GCD_dispatch_async {
NSLog(@"begin: %@",[NSThread currentThread]);
for (int i = 0; i < 10; i++) {
__block int index = i;
dispatch_async(dispatch_queue_create("socket msg recv queue", NULL), ^{
sleep(1);
NSLog(@"执行第%zd次操作,线程:%@",index, [NSThread currentThread]);
});
}
// count: 10
// begin: 02:52:54.213
// end : 02:52:55.231
// time : ≈ 1s
}
但是像上面那么用, 当用在接收socket消息时, 由于消息收的很快,短时间会开N个线程,而且在里面处理耗时任务时, 没等到达到60多个线程的上限, 可能主线程资源就被挤掉, 造成开篇所示的现象了 crash or stutter
【当然,用GCDSocket时,你只需传一个异步串行队列进去,内部在消息回调时只会开一个异步线程,不会n个n个的开了】
所以, 用时需谨慎。 那么,怎么实现线程数目的控制,这里有答案👇
< 1 >. 当然, 多线程管理, 首选NSOperation, SD、AFN等都在用, 它是对GCD的一层封装,方便线程的管理
// NSOperationQueue
- (void)test_NSOperationQueue_withMaxTheadCount:(NSInteger) max {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:max];
NSLog(@"begin: %@",[NSThread currentThread]);
for (int i = 0; i < 10; i++) {
__block int index = i;
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^(){
sleep(1);
NSLog(@"执行第%zd次操作,线程:%@",index, [NSThread currentThread]);
}];
[queue addOperation:operation];
}
// max : 2 10
// begin: 02:29:49.548 02:41:20.911
// end : 02:29:54.806 02:41:21.985
// time : ≈ 5.26s ≈ 1s
}
< 2 >. 通过信号量semaphore
控制
// GCD, semaphore
- (void)test_GCD_semaphore_WithMaxTheadCount:(NSInteger) max {
dispatch_queue_t workConcurrentQueue = dispatch_queue_create("cccccccc", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t serialQueue = dispatch_queue_create("sssssssss",DISPATCH_QUEUE_SERIAL);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(max); // create semaphore: value = 3
NSLog(@"begin: %@",[NSThread currentThread]);
for (int i = 0; i < 10; i++) {
__block int index = i;
dispatch_async(serialQueue, ^{
//value -1, If value < 0, then wait here; else value >= 0, then pass
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(workConcurrentQueue, ^{
sleep(1);
NSLog(@"执行第%zd次操作,线程:%@",index, [NSThread currentThread]);
dispatch_semaphore_signal(semaphore);}); // Perform value +1
});
}
NSLog(@"主线程...!");
// max : 2 10
// begin: 03:02:45.309 03:05:53.301
// end : 03:02:50.332 03:05:54.309
// time : ≈ 5.03s ≈ 1s
}
< 3 >. 根据<2> 封装成 QSDispatchQueue, 使用如下👇
// QSDispatchQueue
- (void)test_QSDispatchQueue_withMaxTheadCount:(NSInteger) max {
dispatch_queue_t concurrentQueue = dispatch_queue_create("socketConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
QSDispatchQueue *queue = [[QSDispatchQueue alloc]initWithQueue:concurrentQueue concurrentCount:max];
NSLog(@"begin: %@",[NSThread currentThread]);
for (int i = 0; i < 10; i++) {
__block int index = i;
[queue async:^{
sleep(1);
NSLog(@"执行第%zd次操作,线程:%@",index, [NSThread currentThread]);
}];
}
// max : 2 10
// begin: 02:37:29.052 02:40:11.811
// end : 02:37:34.069 02:40:12.816
// time : ≈ 5.1s ≈ 1s
}
< 4 >. 通过dispatch_group_t
控制
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self request1:^{
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[self request2:^{
dispatch_group_leave(group);
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//1、2两请求完毕
});
//创建一个group
dispatch_group_t group = dispatch_group_create( );
// for循环遍历各个元素执行操作
for (NSURL *url in arrayURLs) {
//异步组分派到并发队列当中
dispatch_group_async(group, concurrent_queue, ^{
/ /根据url去下载图片
NSLog(@"url is %@",url) ;
});
}
dispatch_group_notify(group, dispatch_get_main_.queue(), ^{
//当添加到组中的所有任务执行完成之后会调用该Block
NSL og(@"所有图片已全部下载完成");
}) ;
懒人下载 👉GitHub Demo