NSOperation和NSOperationQueue的一些基本操作
当初学习多线程这一块的时候,时间比较匆忙,没有细细考虑,而今重新学一次,算是复习和总结吧。
#import "ViewController.h" @interface ViewController () @end @implementation ViewController /*
通过调用[self invocationOperation]的log,可以发现,在不加入queue的情况下,默认是在主线程中同步执行操作的 而通过调用[self blockOperation]的log,可以发现,在不加入queue的情况下,同一个block里的任务是,默认是在主线程中同步执行操作的。但是,如果有多个block操作,会是并发、异步执行的 NSBlockOperation 可以设置监听,调用[self opeationListen] 在完成任务后,可以设置完成这个任务想要执行的操作 调用[self operationQueue]的log,可以发现,在不设置依赖的情况下,任务是并发、异步执行的 而设置依赖的情况下,设置了依赖的任务按照所设定的顺序调用,而没有设置的,则是并发、异步执行
*/
- (void)viewDidLoad {
[super viewDidLoad]; [self opeationListen];
} - (void)invocationOperation
{
NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downLoadImage) object:nil];
[invocation start];
} - (void)blockOperation
{
NSBlockOperation *blockOperation = [[NSBlockOperation alloc] init]; [blockOperation addExecutionBlock:^{
for (int i = ; i<; i++) {
NSLog(@"blockOperation------下载图片1---%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.1];
}
}]; [blockOperation addExecutionBlock:^{
NSLog(@"blockOperation__2 --- %@",[NSThread currentThread]);
}]; [blockOperation addExecutionBlock:^{
NSLog(@"blockOperation__3 --- %@",[NSThread currentThread]);
}]; [blockOperation start];
} - (void)opeationListen
{
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
for (int i = ; i<; i++) {
NSLog(@"opeationListen------下载图片---%@", [NSThread currentThread]);
}
}]; blockOperation.completionBlock = ^{
// ...下载完图片后想做事情
NSLog(@"NSBlockOperation------下载图片完毕---%@", [NSThread currentThread]);
}; [blockOperation start];
} - (void)downLoadImage
{
for (int i = ; i < ; i++) {
NSLog(@"downLoadImage----正在下载----image---%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:0.2];
}
} - (void)run
{
for (int i = ; i<; i++) {
NSLog(@"run---正在跑步-----%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.2];
}
}
- (void)operationQueue
{
NSInvocationOperation *invocationOperation_1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downLoadImage) object:nil]; NSInvocationOperation *invocationOperation_2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; NSBlockOperation *blockOperation_1 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = ; i < ; i++) {
NSLog(@"blockOperation_1------下载图片---%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.1];
}
}]; NSBlockOperation *blockOperation_2 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = ; i < ; i++) {
NSLog(@"blockOperation_2------下载视频---%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:0.1];
}
}]; //设置依赖
[blockOperation_2 addDependency:blockOperation_1]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; //设置最大并发数(一般设置为2~~3个)
queue.maxConcurrentOperationCount = ; [queue addOperations:@[invocationOperation_1, invocationOperation_2, blockOperation_1, blockOperation_2] waitUntilFinished:NO];
}
@end
NSOperation和NSOperationQueue的一些基本操作的更多相关文章
- GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例
转:http://www.tuicool.com/articles/NVVnMn (1)GCD实现的同步异步.串行并行. ——同步sync应用场景:用户登录,利用阻塞 ——串行异步应用场景:下载等耗时 ...
- 多线程之NSOperation和NSOperationQueue
这篇文章里我将不过多的谈及理论知识,这些东西会的自然会,不会的,看多了也是云里雾里.下面我讲更多的用代码+注释的方式来讲如何使用NSOperation和NSOperationQueue. 1.NSOp ...
- 用NSOperation和NSOperationQueue实现多线程编程
1.上一讲简单介绍了NSThread的使用,虽然也可以实现多线程编程,但是需要我们去管理线程的生命周期,还要考虑线程同步.加锁问题,造成一些性能上的开销.我们也可以配合使用NSOperation和NS ...
- OC - 14.NSOperation与NSOperationQueue
简介 通过NSOperation与NSOperationQueue的组合也能实现多线程 通常将任务封装成NSOperation对象,并将对象添加到NSOperationQueue中实现 NSOpera ...
- 多线程下的NSOperation和NSOperationQueue的使用
多线程下的NSOperation和NSOperationQueue的使用 NSOperation和NSOperationQueue的介绍: NSOperation是Cocoa中的一个抽象类,用来封装单 ...
- Swift - 多线程实现方式(2) - NSOperation和NSOperationQueue
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- iOS 多线程 NSOperation、NSOperationQueue
1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...
- NSOperation、NSOperationQueue(III)
NSOperation.NSOperationQueue 常用属性和方法归纳 NSOperation 常用属性和方法 a. 取消操作方法 //可取消操作,实质是标记 isCancelled 状态. - ...
- NSOperation、NSOperationQueue(II)
NSOperationQueue 控制串行执行.并发执行 NSOperationQueue 创建的自定义队列同时具有串行.并发功能 这里有个关键属性 maxConcurrentOperationCou ...
随机推荐
- 转:SpringMVC中日期格式的转换
解决日期提交转换异常的问题 由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑定.前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestM ...
- 在Linux上yum安装运行Redis,只能安装2.4.10(主从)
Installing Redis on CentOS 6.4 First, install the epel repo sudo rpm -Uvh http://download.fedoraproj ...
- Docker 建立镜像流程
1.docker build from dockerfile http://www.runoob.com/docker/docker-build-command.html $ docker build ...
- 清理kafka zookeeper
; ; /; rm -rf /app/zookeeper/logs/*; rm -rf /app/pet_kafka_emds2_cluster/kafka-logs/*; rm -rf /app/p ...
- get the code of function in matlab
>> edit <function>>> edit perform
- linux c select函数使用求解释
代码非常easy,就是发送c语言发送http请求.但 i= read(sockfd, buf, BUFSIZE-1); 能够正常执行, 替换为i= Read(sockfd, buf, B ...
- 网络协议-网络分层、TCP/UDP、TCP三次握手和四次挥手
网络的五层划分是什么? 应用层,常见协议:HTTP.FTP 传输层,常见协议:TCP.UDP 网络层,常见协议:IP 链路层 物理层 TCP 和 UDP 的区别是什么 TCP/UDP 都属于传输层的协 ...
- RecyclerView中实现headerView,footerView功能
之前用com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader 不过局限性有点大. (com.bartoszlipinski.recycler ...
- 【Unity】8.4 扩展UnityGUI
分类:Unity.C#.VS2015 创建日期:2016-04-27 一.简介 有很多种方法可以补充和扩展 UnityGUI 以满足您的需求.你可以混合和创建控件,并且可以有多种方法来规定用户 GUI ...
- 有关https安全的相关内容介绍
Https 介绍什么是Https HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道.简单讲是H ...