(五十六)iOS多线程之NSOperation
NSOpertation是一套OC的API,是对GCD进行的Cocoa抽象。
NSOperation有两种不同类型的队列,主队列和自定义队列。
主队列运行于主线程上,自定义队列在后台运行。
【NSBlockOperation】
通过Block创建任务,下面比较主队列和自定义队列的区别:
将自定义队列声明为成员变量,并进行初始化:
@property (nonatomic, strong) NSOperationQueue *myqueue;
self.myqueue = [[NSOperationQueue alloc] init];
获取主队列的方法为[NSOperationQueue mainQueue]。
队列有一个方法addOperationWithBlock方法用于添加一个用Block描述的任务。
具体代码为:
- (void)NSBlockOperation{
// 自定义队列在子线程中运行。
[self.myqueue addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
}];
// 主队列任务在主线程中运行。
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
}];
}
执行这个方法,得到的结果如下,可见与上面的描述相符。
2015-02-17 10:46:15.308 NSOpertaion[709:17138] <NSThread: 0x7b9aa440>{number = 2, name = (null)}
2015-02-17 10:46:15.319 NSOpertaion[709:17067] <NSThread: 0x7b978550>{number = 1, name = main}
【NSInvocationOperation】
需要定义一个回调方法,好处是可以接收一个id类型的object作为消息。
例如:
- (void)NSInvocationOperation{
NSDictionary *msg = @{@"name" : @"op",@"message" : @"hello"};
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(InvocationCall:) object:msg];
[self.myqueue addOperation:op];
}
实现回调方法:
- (void)InvocationCall:(id)obj{
NSLog(@"%@ with object %@",[NSThread currentThread],obj);
}
调用后,打印的结果如下,可以看到object对象被传了过来。
2015-02-17 10:56:03.764 NSOpertaion[812:26537] <NSThread: 0x7ba6eb10>{number = 2, name = (null)} with object {
message = hello;
name = op;
}
【任务执行顺序】
在默认情况下,自定义队列是并行队列,执行无序;而主队列为串行队列,有序执行。下面进行实验验证说法:
// 自定义队列在子线程中运行。
for (int i = 0; i < 9; i++) {
[self.myqueue addOperationWithBlock:^{
NSLog(@"%@ with no %d",[NSThread currentThread],i);
}];
} // 主队列任务在主线程中运行。
for (int i = 0; i < 9; i++) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"%@ with no %d",[NSThread currentThread],i);
}];
}
执行结果如下,可见与上面的描述相同。
2015-02-17 11:04:08.421 NSOpertaion[919:32337] <NSThread: 0x7bf91f50>{number = 9, name = (null)} with no 6
2015-02-17 11:04:08.421 NSOpertaion[919:32331] <NSThread: 0x7bf91c10>{number = 4, name = (null)} with no 1
2015-02-17 11:04:08.421 NSOpertaion[919:32338] <NSThread: 0x7bf915c0>{number = 6, name = (null)} with no 5
2015-02-17 11:04:08.421 NSOpertaion[919:32336] <NSThread: 0x7dab62c0>{number = 7, name = (null)} with no 4
2015-02-17 11:04:08.421 NSOpertaion[919:32339] <NSThread: 0x7dab61b0>{number = 3, name = (null)} with no 7
2015-02-17 11:04:08.421 NSOpertaion[919:32330] <NSThread: 0x7dab6110>{number = 2, name = (null)} with no 0
2015-02-17 11:04:08.421 NSOpertaion[919:32341] <NSThread: 0x7d97b3f0>{number = 5, name = (null)} with no 8
2015-02-17 11:04:08.421 NSOpertaion[919:32328] <NSThread: 0x7be6d450>{number = 10, name = (null)} with no 3
2015-02-17 11:04:08.421 NSOpertaion[919:32329] <NSThread: 0x7dab6450>{number = 8, name = (null)} with no 2
2015-02-17 11:04:08.448 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 0
2015-02-17 11:04:08.449 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 1
2015-02-17 11:04:08.449 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 2
2015-02-17 11:04:08.449 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 3
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 4
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 5
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 6
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 7
2015-02-17 11:04:08.450 NSOpertaion[919:32281] <NSThread: 0x7d96f040>{number = 1, name = main} with no 8
【自定义队列顺序执行】
使用NSBlockOperation对象的addDependency设置依赖关系,只有依赖的对象执行完毕后,自己才能执行。
例如下面的例子,三个任务要顺序执行,先下载,再处理,最后显示,通过这样的设定可以保证顺序:
- (void)SerialOperation{
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"下载");
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"处理");
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"显示");
}];
[op2 addDependency:op1];
[op3 addDependency:op2];
[self.myqueue addOperation:op1];
[self.myqueue addOperation:op2];
[self.myqueue addOperation:op3];
}
注意这个执行和串行队列的异步任务不同点是,串行队列的异步任务仅仅开一个线程;自定义队列的顺序执行可能开辟多个但不会太多个线程。
注意上面的代码有一定的问题,因为显示只有主线程可以处理,所以op3应该放入主线程。
Tip:依赖关系可以跨队列,因此op3依赖op2在主线程中仍然有效,只需要修改op3的入队代码为:
[[NSOperationQueue mainQueue] addOperation:op3];
Tip:注意避开循环依赖,程序会崩溃。
【设定多线程的最大开销】
设定同时执行的最大线程数:通过队列的setMaxConcurrentOperationCount方法来设定,例如:
[self.myqueue setMaxConcurrentOperationCount:3];
应用场景:网络通信,例如3G开3个子线程,WIFI开6个子线程。
Tip:线程的开销主要是CPU和内存,还会耗电,因此应该考虑软件的能耗。
Tip:AFNetworing的底层是使用GCD开发的,接口是NSOperation。
(五十六)iOS多线程之NSOperation的更多相关文章
- iOS 多线程之NSOperation篇举例详解
这篇博客是接着总篇iOS GCD NSOperation NSThread等多线程各种举例详解写的一个支篇.总篇也包含了此文的链接.本文讲解的知识点有NSBlockOperationClick,队列, ...
- iOS多线程之NSOperation详解
使用NSOperation和NSOperationQueue进行多线程开发,只要将一个NSOperation(实际开发中需要使用其子类 NSInvocationOperation,NSBlockOpe ...
- IOS多线程之NSOperation学习总结
NSOperation简介 1.NSOperation的作用 配合使用NSOperation和NSOperationQueue也能实现多线程编程 2.NSOperation和NSOperationQu ...
- iOS多线程之NSOperation,NSOperationQueue
使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也 ...
- iOS多线程之NSOperation和NSOperationQueue的使用
一:NSOperation 两个子类+重写main方法 NSInvocationOperation NSBlockOperation 有个类方法 BlockOprationWith: 还有就是自己个子 ...
- ios多线程之NSOperation
使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也 ...
- iOS多线程之8.NSOPeration的其他用法
本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...
- iOS多线程之GCD小记
iOS多线程之GCD小记 iOS多线程方案简介 从各种资料中了解到,iOS中目前有4套多线程的方案,分别是下列4中: 1.Pthreads 这是一套可以在很多操作系统上通用的多线程API,是基于C语言 ...
- 【Visual C++】游戏开发五十六 浅墨DirectX教程二十三 打造游戏GUI界面(一)
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/16384009 作者:毛星云 ...
随机推荐
- weblogic静默方式创建域
创建域目录:mkdir -p /home/weblogic/Oracle/Middleware/user_projects/domains/base_domain/ 创建文件: create_doma ...
- 剑指架构师系列-ActiveMQ队列的使用
安装ActiveMQ只需要下载包后解压,然后就可以启动与关闭ActiveMQ了,如下: ./activemq start ./activemq stop 访问管理页面: http://10.10.20 ...
- 转:rabbitmq——用户管理
原文:http://my.oschina.net/hncscwc/blog/262246?p={{currentPage-1}} 安装最新版本的rabbitmq(3.3.1),并启用managemen ...
- ACM Where is the Marble?
Description Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers ...
- Bootstrap3 表单-水平排列的表单
通过为表单添加 .form-horizontal 类,并联合使用 Bootstrap 预置的栅格类,可以将 label 标签和控件组水平并排布局.这样做将改变 .form-group 的行为,使其表现 ...
- blog写作心得体会
虽然写blog也挺久了,写出来的东西自己回顾的时候也会怀疑读者是否能看的明白,还是有种流水账的感觉,以后希望多从读者的角度出发.下面记录一些以后写博客的注意点. 具体关于某种技术点的小知识还有碰到的各 ...
- IOS开发初体验
IOS开发初体验 搭建开发环境 不多说什么了,开发环境的搭建太简单了,上App Store搜索XCode下载就行了,说多了都是眼泪 创建第一个IOS项目--HolleIOS 创建工程 选择工程创建位置 ...
- Spring之Enterprise JavaBeans (EJB) integration
原文地址:需要FQ https://translate.google.com/translate?hl=zh-CN&sl=zh-CN&tl=zh-CN&u=http%3A%2F ...
- C控制台实现模拟平抛运动算法
平抛运动这个相信读了高中物理都知道这个概念了,详细的我就不说了,不明白的看看百度: 平抛运动 接下来看看用控制台实现的平抛运动算法: #include <stdio.h> #include ...
- Android初级教程理论知识(第九章多媒体编程)
多媒体概念 文字.图片.音频.视频 计算机图片大小的计算 图片大小 = 图片的总像素 * 每个像素占用的大小 单色图:每个像素占用1/8个字节 16色图:每个像素占用1/2个字节 256色图:每个像素 ...