iOS多线程编程之NSOperation和NSOperationQueue的使用
2015-10-29 09:05
585 查看
(一)队列里可以加入很多个NSOperation, 可以把NSOperationQueue看作一个线程池,可往线程池中添加操作(NSOperation)到队列中。线程池中的线程可看作消费者,从队列中取走操作,并执行它。
(二)队列类型
1、主队列:添加到主队列的操作,都会放到主线程中执行。
2、非主队列:添加到非主队列的操作,都会放到子线程中执行。
(三)队列添加任务的方式
(四)基本用法
(四)常见用法
1、最大并发数
2、取消所有操作
3、暂停所有操作
4、恢复所有操作
5、依赖:可以在不同queue的NSOperationQueue之间建立依赖关系
(二)队列类型
1、主队列:添加到主队列的操作,都会放到主线程中执行。
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.imageView.image = image; }];
2、非主队列:添加到非主队列的操作,都会放到子线程中执行。
[[[NSOperationQueue alloc] init] addOperationWithBlock:^{ NSLog(@"下载图片3---%@", [NSThread currentThread]); }];
(三)队列添加任务的方式
- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block
(四)基本用法
// 1.创建一个队列(非主队列) NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // 2.添加操作到队列中(自动异步执行任务,并发) NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"下载图片1---%@", [NSThread currentThread]); }]; NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"下载图片2---%@", [NSThread currentThread]); }]; [queue addOperation:operation1]; [queue addOperation:operation2]; [queue addOperationWithBlock:^{ NSLog(@"下载图片3---%@", [NSThread currentThread]); }]; // 3个操作并发执行
(四)常见用法
1、最大并发数
// 1.创建一个队列(非主队列) NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // 2.设置最大并发(最多同时并发执行3个任务) queue.maxConcurrentOperationCount = 3; [queue setMaxConcurrentOperationCount:3];
2、取消所有操作
[queue cancelAllOperations]; // 取消队列中的所有任务(不可恢复)
3、暂停所有操作
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [queue setSuspended:YES]; // 暂停队列中的所有任务 }
4、恢复所有操作
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [queue setSuspended:NO]; // 恢复队列中的所有任务 }
5、依赖:可以在不同queue的NSOperationQueue之间建立依赖关系
/** 假设有A、B、C三个操作,要求: 1. 3个操作都异步执行 2. 操作C依赖于操作B 3. 操作B依赖于操作A */ // 1.创建一个队列(非主队列) NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // 2.创建3个操作 NSBlockOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"A1---%@", [NSThread currentThread]); }]; NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"B---%@", [NSThread currentThread]); }]; NSBlockOperation *operationC = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"C---%@", [NSThread currentThread]); }]; // 设置依赖 [operationB addDependency:operationA]; [operationC addDependency:operationB]; // 3.添加操作到队列中(自动异步执行任务) [queue addOperation:operationC]; [queue addOperation:operationA]; [queue addOperation:operationB];6、线程之间的通信
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
// 1.异步下载图片
NSURL *url = [NSURL URLWithString:@"http://d.hiphotos.baidu.com/image/pic/item/37d3d539b6003af3290eaf5d362ac65c1038b652.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
// 2.回到主线程,显示图片
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.imageView.image = image; }];
}];
相关文章推荐
- UIImageView 获取图片的 宽 高
- error:unknown filesystem grub rescue
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
- 通用固定长度编码格式的字符串查找算法的实现
- UI基础之 --------- UITableView
- SpriteBuilder中节点位置类型为百分比时不能定位的解决
- SpriteBuilder中节点位置类型为百分比时不能定位的解决
- SpriteBuilder中节点位置类型为百分比时不能定位的解决
- 桌面虚拟化,你还担心什么?
- extensions添加的方法是requires
- iOS输入框UITextView随键盘UIkeyboard弹出与消失改变Rect
- How the performance impacts your revenue-性能影响营收
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- UINavigationController导航控制器一
- LeetCode——Unique Path
- easyui-combobox---ajax获取数据库JSON数据,实现搜索框实时显示模糊搜索结果
- UIColor的RGB定义颜色(灰色)
- 基于EasyUI的Web开发框架
- java集合框架 arrayblockingqueue应用分析