iOS学习笔记-117.多线程16——NSOperationQueue最大并发数
2017-09-05 23:07
459 查看
多线程16NSOperationQueue最大并发数
一最大并发数
1 什么是并发数
2 最大并发数的相关方法
3 最大并发数说明
二代码示例
三结果
比如,同时开3个线程执行3个任务,并发数就是3
一最大并发数
1 什么是并发数
2 最大并发数的相关方法
3 最大并发数说明
二代码示例
三结果
多线程16——NSOperationQueue最大并发数
一、最大并发数
1.1 什么是并发数
同时执行的任务数比如,同时开3个线程执行3个任务,并发数就是3
1.2 最大并发数的相关方法
- (NSInteger)maxConcurrentOperationCount; - (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
1.3 最大并发数说明
//同一时间最多有多少个任务可以执行 //串行执行任务!=只开一条线程 (线程同步) // maxConcurrentOperationCount >1 那么就是并发队列 // maxConcurrentOperationCount == 1 那就是串行队列 // maxConcurrentOperationCount == 0 不会执行任务 // maxConcurrentOperationCount == -1 特殊意义 最大值 表示不受限制
二、代码示例
// // QWMViewController2.m // 03_UIview85多线程_NSOperation // // Created by 杞文明 on 17/9/4. // Copyright © 2017年 杞文明. All rights reserved. // #import "QWMViewController2.h" @interface QWMViewController2 () @end @implementation QWMViewController2 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self invocationOperationWithQueue]; } -(void)invocationOperationWithQueue{ //1.创建操作,封装任务 NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download:) object:@"1"]; NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download:) object:@"2"]; NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download:) object:@"3"]; //2.创建队列 //设置最大并发数量 maxConcurrentOperationCount //同一时间最多有多少个任务可以执行 //串行执行任务!=只开一条线程 (线程同步) // maxConcurrentOperationCount >1 那么就是并发队列 // maxConcurrentOperationCount == 1 那就是串行队列 // maxConcurrentOperationCount == 0 不会执行任务 // maxConcurrentOperationCount == -1 特殊意义 最大值 表示不受限制 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; queue.maxConcurrentOperationCount = 1; //3.添加任务到队列中 [queue addOperation:op1]; [queue addOperation:op2]; [queue addOperation:op3]; } -(void)download:(NSString*)params{ NSLog(@"--%@----%@",params,[NSThread currentThread]); } @end
三、结果
[19573:104374] --1----<NSThread: 0x60000026c800>{number = 6, name = (null)} [19573:104374] --2----<NSThread: 0x60000026c800>{number = 6, name = (null)} [19573:104374] --3----<NSThread: 0x60000026c800>{number = 6, name = (null)}
相关文章推荐
- iOS学习笔记-115.多线程14——NSOperationQueue和NSBlockOperation合用实现多线程
- iOS学习笔记-116.多线程15——NSOperationQueue和自定义NSOperation合用实现多线程
- iOS学习笔记-114.多线程13——NSOperationQueue和NSInvocationOperation合用实现多线程
- iOS学习笔记-118.多线程17——NSOperationQueue队列的取消、暂停、恢复
- iOS 多线程编程<十、NSOperationQueue最大并发量、暂停、取消>
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- 多线程学习笔记3-NSOperationQueue
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- iOS学习笔记15—Cocoa:NSOperation和NSOperationQueue
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- IOS学习笔记23—多线程之NSThread、NSOperation、NSInvocationOperation
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS 多线程 简单学习NSThread NSOperation GCD
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS-----使用NSOperation与NSOperationQueue实现多线程
- IOS多线程编程之NSOperation和NSOperationQueue的使用
- IOS_多线程编程3 - NSOperationQueue
- iOS多线程编程之NSOperation和NSOperationQueue的使用(转自容芳志专栏)
- 【iOS学习笔记 16-2-22】iOS多线程知识总结