iOS 开发多线程 —— NSOperation
本文是根据文顶顶老师的博客学习而来,转载地址:http://www.cnblogs.com/wendingding/p/3809042.html
一、NSOperation简介
1.简单说明
NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤:
(1)先将需要执行的操作封装到一个NSOperation对象中
(2)然后将NSOperation对象添加到NSOperationQueue中
(3)系统会⾃动将NSOperationQueue中的NSOperation取出来
(4)将取出的NSOperation封装的操作放到⼀条新线程中执⾏
2.NSOperation的子类
NSOperation是个抽象类,并不具备封装操作的能力,必须使⽤它的子类
使用NSOperation⼦类的方式有3种:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自定义子类继承NSOperation,实现内部相应的⽅法
NSInvocationOperation:
示例代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil];
[operation start];
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil];
[operation2 start];
}
-(void)thread1{
NSLog(@"currentThread = %@",[NSThread currentThread]);
}
-(void)thread2{
NSLog(@"currentThread2 = %@",[NSThread currentThread]);
}
currentThread = <NSThread: 0x17406d780>{number = , name = main}
currentThread2 = <NSThread: 0x17406d780>{number = , name = main}
可以看见,并没有开启新的线程.
注意:操作对象默认在主线程中执行,只有添加到队列中才会开启新的线程。即默认情况下,如果操作没有放到队列中queue中,都是同步执行。只有将NSOperation放到一个NSOperationQueue中,才会异步执行操作
NSBlockOperation:
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"operation = %@",[NSThread currentThread]);
}];
[operation3 start];
看打印结果:
operation = <NSThread: 0x1700728c0>{number = , name = main}
注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作
例如:
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"operation = %@",[NSThread currentThread]);
}];
[operation3 addExecutionBlock:^{
NSLog(@"operation2 = %@",[NSThread currentThread]);
}];
[operation3 start];
然后看打印结果:
operation = <NSThread: 0x1700706c0>{number = , name = main}
operation2 = <NSThread: 0x170075a40>{number = , name = (null)}
NSOperationQueue:
NSOperationQueue的作⽤:NSOperation可以调⽤start⽅法来执⾏任务,但默认是同步执行的
如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作
添加操作到NSOperationQueue中,自动执行操作,自动开启线程
示例:
- (void)viewDidLoad {
[super viewDidLoad];
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil];
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil];
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"currentThread3 = %@",[NSThread currentThread]);
}];
[operation3 addExecutionBlock:^{
NSLog(@"currentThread3_1 = %@",[NSThread currentThread]);
}];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//把操作添加到队列中
//第一种方式
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
//第二种方式
[queue addOperationWithBlock:^{
NSLog(@"currentThread4 = %@",[NSThread currentThread]);
}];
}
-(void)thread1{
NSLog(@"currentThread1 = %@",[NSThread currentThread]);
}
-(void)thread2{
NSLog(@"currentThread2 = %@",[NSThread currentThread]);
}
查看结果:
currentThread1 = <NSThread: 0x17006f440>{number = , name = (null)}
currentThread2 = <NSThread: 0x17007d9c0>{number = , name = (null)}
currentThread3_1 = <NSThread: 0x17007d9c0>{number = , name = (null)}
currentThread3 = <NSThread: 0x17006f440>{number = , name = (null)}
currentThread4 = <NSThread: 0x17007d9c0>{number = , name = (null)}
iOS 开发多线程 —— NSOperation的更多相关文章
- iOS开发-多线程NSOperation和NSOperationQueue
上一篇文章稍微提及了一下NSThread的使用,NSThread能直观地控制线程对象,不过需要自己管理线程的生命周期,线程同步,用起来比较繁琐,而且比较容易出错.不过Apple给出了自己的解决方案NS ...
- iOS开发多线程篇—NSOperation简单介绍
iOS开发多线程篇—NSOperation简单介绍 一.NSOperation简介 1.简单说明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现 ...
- iOS开发多线程篇—NSOperation基本操作
iOS开发多线程篇—NSOperation基本操作 一.并发数 (1)并发数:同时执⾏行的任务数.比如,同时开3个线程执行3个任务,并发数就是3 (2)最大并发数:同一时间最多只能执行的任务的个数. ...
- iOS开发多线程篇—自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
- iOS开发多线程篇 09 —NSOperation简单介绍
iOS开发多线程篇—NSOperation简单介绍 一.NSOperation简介 1.简单说明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现 ...
- iOS开发多线程篇 11 —自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
- iOS开发多线程篇 10 —NSOperation基本操作
iOS开发多线程篇—NSOperation基本操作 一.并发数 (1)并发数:同时执⾏行的任务数.比如,同时开3个线程执行3个任务,并发数就是3 (2)最大并发数:同一时间最多只能执行的任务的个数. ...
- iOS开发——多线程OC篇&多线程详解
多线程详解 前面介绍了多线程的各种方式及其使用,这里补一点关于多线程的概念及相关技巧与使用,相信前面不懂的地方看了这里之后你就对多线程基本上没有什么问题了! 1——首先ios开发多线程中必须了解的概念 ...
- 【iOS开发】NSOperation简单介绍
iOS开发多线程篇—NSOperation简单介绍 一.NSOperation简介 1.简单说明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现 ...
随机推荐
- (转)浅谈AIX下IPFilter防火墙
1 序言 AIX操作系统发行至今,经历数个版本,功能不断增强,就安全方面IP Security也变化不少,如动作中增加了If等功能,但这次暂且讨论配置防火墙策略及防火墙的基本操作,其他高级功能待 ...
- chrome版本与对应的chromedriver驱动【转载】
chrome版本与对应的谷歌驱动(chromedriver) 1.下载chromedriver:http://chromedriver.storage.googleapis.com/index.htm ...
- spring与struts简单整合案例
Spring,负责对象对象创建 Struts, 用Action处理请求 Spring与Struts框架整合, 关键点:让struts框架action对象的创建,交给spring完成! 步骤: 1)引入 ...
- [开源项目]Shell4Win,一个在Windows下执行shell命令的解释器
背景 顺利拿到心目中的理想offer之后,心里的负担一下减轻了很多,希望利用还没毕业之前这段难得的悠闲时间做一点有意义的事情.于是希望能做一个长久以来都想做的开源项目,就是题中提到的Windows下的 ...
- 揭开Future的神秘面纱——任务执行
前言 此文承接之前的博文 解开Future的神秘面纱之取消任务 补充一些任务执行的一些细节,并从全局介绍程序的运行情况. 系列目录 揭开Future的神秘面纱——任务取消 揭开Future的神秘面纱— ...
- Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <-错误解决方案
做项目的时候因为懒,在写service时直接复制了控制器的依赖注入,之后就出现了这个错误,查了半天. 解决其实很简单,删除掉service中注入的$scope即可.
- vue路由管理-保留滚动位置功能、按需加载模块名自定义
路由管理:保留滚动位置 其实现与组件的keep-alive相关,仅设置了keep-aive的页面,实施保留回退位置能力. keep-alive介绍 作用 把切换出去的组件保留在内存中,可以保留它的状态 ...
- ActiveMQ与Spring整合-MessageListener
消费者,使用监听的实现方式. 1. pom.xml 2. 生产者 package org.ygy.mq.lesson04; import javax.jms.JMSException; import ...
- PHP strlen()函数和strpos()函数
strlen() 函数返回字符串的长度(字符数) 代码: <?php echo strlen("Hello world!"); ?> 上面的代码将输出:12 ...
- 呼叫WCF Service的方法出现Method not allowed异常
asp.net mvc练习程序,经常性在家里电脑,笔记本或是公司的电脑之间拷贝与粘贴,如果忘记携带最新的练习程序,一些小功能只能重新写了.如前一篇<ASP.NET MVC呼叫WCF Servic ...