NSOperation, NSOperationQueue 原理探析
通过GNUstep的Foundation来尝试探索下NSOperation,NSOperationQueue
示例程序
写一个简单的程序
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configurationQueue];
LDNSOperation *operation = [[LDNSOperation alloc] init];
[self.operationQueue addOperation:operation];
[NSThread sleepForTimeInterval:3];
[operation cancel];
}
-(void)configurationQueue{
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 4;
}
LDNSOperation为NSOperation的子类,重写strat方法
-(void)start{
while (true) {
if(self.cancelled){
NSLog(@"已经取消");
return;
}
NSLog(@"start");
[NSThread sleepForTimeInterval:1];
}
}
实现的效果很简单,打印三个strat,然后结束operation。
初探
根据阅读GNU的源码,也只能是猜想,但是尝试了很多方法,没有找到可以验证的方案,但是实现原理上还是有很多相似处的。
NSOperation有三种状态,isReady, isExecuting, isFinished.
很多其他的参数也会随着NSOperationQueue的addOperation操作而变化着。
例如:
[self.operationQueue addOperation:operation];
添加一个未完成的NSOperation,其实就是将NSOperation添加到一个动态数组当中
- (void) addOperation: (NSOperation *)op
{
if (op == nil || NO == [op isKindOfClass: [NSOperation class]])
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-%@] object is not an NSOperation",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
}
[internal->lock lock];
if (NSNotFound == [internal->operations indexOfObjectIdenticalTo: op]
&& NO == [op isFinished])
{
[op addObserver: self
forKeyPath: @"isReady"
options: NSKeyValueObservingOptionNew
context: NULL];
[self willChangeValueForKey: @"operations"];
[self willChangeValueForKey: @"operationCount"];
[internal->operations addObject: op];
[self didChangeValueForKey: @"operationCount"];
[self didChangeValueForKey: @"operations"];
if (YES == [op isReady])
{
[self observeValueForKeyPath: @"isReady"
ofObject: op
change: nil
context: nil];
}
}
[internal->lock unlock];
}
internal就是一个内部类,指代的就是NSOperationQueue,这里也是一个KVO的手动通知,进行operations,与operationCount的改变通知。
这里lock是NSRecursiveLock(递归锁),原因我猜测是因为递归锁的特性是可以被同一线程多次请求,而不会引起死锁。同一线程的多次addOperation操做情况还是很多的。
每一次属性的变化,都伴随着其他属性的改变
- (void) observeValueForKeyPath: (NSString *)keyPath
ofObject: (id)object
change: (NSDictionary *)change
context: (void *)context
{
[internal->lock lock];
if (YES == [object isFinished])
{
internal->executing--;
[object removeObserver: self
forKeyPath: @"isFinished"];
[internal->lock unlock];
[self willChangeValueForKey: @"operations"];
[self willChangeValueForKey: @"operationCount"];
[internal->lock lock];
[internal->operations removeObjectIdenticalTo: object];
[internal->lock unlock];
[self didChangeValueForKey: @"operationCount"];
[self didChangeValueForKey: @"operations"];
}
else if (YES == [object isReady])
{
[object removeObserver: self
forKeyPath: @"isReady"];
[internal->waiting addObject: object];
[internal->lock unlock];
}
[self _execute];
}
其实在maxConcurrentOperationCount和suspended的setter方法里面都会调用_execute方法,以及在其他属性如operationCount、operations、值发生变化的时候都会调用它。
那么_execute究竟是什么?
整个源码都拿上来
- (void) _execute
{
NSInteger max;
[internal->lock lock];
max = [self maxConcurrentOperationCount];
if (NSOperationQueueDefaultMaxConcurrentOperationCount == max)
{
max = maxConcurrent;
}
while (NO == [self isSuspended]
&& max > internal->executing
&& [internal->waiting count] > 0)
{
NSOperation *op;
op = [internal->waiting objectAtIndex: 0];
[internal->waiting removeObjectAtIndex: 0];
[op addObserver: self
forKeyPath: @"isFinished"
options: NSKeyValueObservingOptionNew
context: NULL];
internal->executing++;
if (YES == [op isConcurrent])
{
[op start];
}
else
{
NSUInteger pending;
[internal->cond lock];
pending = [internal->starting count];
[internal->starting addObject: op];
if (0 == internal->threadCount
|| (pending > 0 && internal->threadCount
{
internal->threadCount++;
[NSThread detachNewThreadSelector: @selector(_thread)
toTarget: self
withObject: nil];
}
/* Tell the thread pool that there is an operation to start.
*/
[internal->cond unlockWithCondition: 1];
}
}
[internal->lock unlock];
}
从源码中可以看到,根据isConcurrent分为直接执行,和非直接执行,isConcurrent为YES的话可以直接执行start操作,但是如果isConcurrent为NO,那么这里使用detachNewThreadSelector来创建新的线程去执行start。
总结下来:
所有的线程都很忙,并且没有达到threadCount的最大值的时候会创建新的线程,这代表queue并不是一个线程,也有可能有几个
_execute就是一个执行队列,依次将等待队列里面的所有operation进行start。
其实对于start函数来说的话,一个NSOperation并没有新创建一条线程,依然操作在[NSThread currentThread]中,感兴趣可以去做一下测试。从源码中也是可以看出来的,
- (void) start
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
double prio = [NSThread threadPriority];
[internal->lock lock];
NS_DURING
{
if (YES == [self isConcurrent])
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-%@] called on concurrent operation",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
}
if (YES == [self isExecuting])
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-%@] called on executing operation",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
}
if (YES == [self isFinished])
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-%@] called on finished operation",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
}
if (NO == [self isReady])
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-%@] called on operation which is not ready",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
}
if (NO == internal->executing)
{
[self willChangeValueForKey: @"isExecuting"];
internal->executing = YES;
[self didChangeValueForKey: @"isExecuting"];
}
}
NS_HANDLER
{
[internal->lock unlock];
[localException raise];
}
NS_ENDHANDLER
[internal->lock unlock];
NS_DURING
{
if (NO == [self isCancelled])
{
[NSThread setThreadPriority: internal->threadPriority];
[self main];
}
}
NS_HANDLER
{
[NSThread setThreadPriority: prio];
[localException raise];
}
NS_ENDHANDLER;
[self _finish];
[pool release];
}
总结
整个过程伴随着很多属性的变化,同步这些属性,KVO在其中起着举足轻重的作用,通过源码也可以发现,NSOperationQueue对NSOperation的处理分为并发和非并发的情况。如果不想采用非并发的形式,我们可以直接自定义子类化,在NSOperationQueue中添加,并且管理就可以了,功能类似线程池的用法。
但是如果想要自定义的NSOperation是并发的仅仅是重写isExecuting、isFinished、isConcurrent、isAsynchronous 这四个方法,isAsynchronous反回YES就可以了吗?
从源码中我们可以看到,NSOperation的start依然使用的[NSThread currentThread]。所以依然需要自己创建,例如:
[NSThread detachNewThreadSelector:@selector(start) toTarget:self withObject:nil];
现在来思考下,也就明白了为什么NSOperationQueue要有两种处理方式了,如果NSOperation支持并发,然后NSOperationQueue在为其分配线程,那就是线程里面又跑了一条线程,这样就很尴尬了,通过isConcurrent可以避免这种现象。
通常在大多数时候我们并不会直接去使用自定义的 NSOperation ,如果操作不复杂,可以直接使用 NSInvocationOperation 和 NSBlockOperation 这两个子类。
如果真的需要使用多线程,通常都会用 NSOperationQueue来处理就可以了。
这里也是仅仅简单的探索了一下,上面的源码是封装的NSThread,但是Apple的实现可能封装的不是NSThread,因为断点后并没有看到跟NSThread相关的东西,还是有很多细节需要去推敲。
转载于:https://www.cnblogs.com/fengmin/p/6108165.html
NSOperation, NSOperationQueue 原理探析相关推荐
- mod php是什么意思,mod_php模式原理探析
1.PHP与Apache工作模式 在传统的LAMP架构中,PHP与Apache交互时,至少有两种方式『运行PHP』: 使用CGI:Apache发送请求至php-cgi进程,php-cgi进程调用PHP ...
- Python 中弱引用的神奇用法与原理探析
文章源地址:神奇的弱引用 | 小菜学编程 背景 开始讨论弱引用( weakref )之前,我们先来看看什么是弱引用?它到底有什么作用? 假设我们有一个多线程程序,并发处理应用数据: # 占用大量资源, ...
- 计算机三维制图论文,对三维动画技术与三维虚拟技术探析论文
对三维动画技术与三维虚拟技术探析论文 摘要: 三维动画技术在目前的发展中,人们在不断的尝试新的方法来更加的完善,它的完善能够更直观的把问题呈现,所以三维技术的应用一直发展至今.通过对三维动画技术与三维 ...
- 中国太阳能热水器市场营销模式探析与品牌格局调研报告2022版
中国太阳能热水器市场营销模式探析与品牌格局调研报告2022版 HS--HS--HS--HS--HS--HS--HS--HS--HS--HS--HS--HS-- [修订日期]:2021年11月 [搜索鸿 ...
- 实录 | 平安人寿资深算法工程师姚晓远:对话生成模型的探析与创新
1 月 10 日(周四)晚 8 点,平安人寿智能平台团队资深算法工程师姚晓远在 PaperWeekly 直播间为大家带来了对话生成模型的探析与创新主题分享,并且介绍了平安人寿基于业务场景的技术探索成果 ...
- 今晚直播 | 平安人寿资深算法工程师姚晓远:对话生成模型的探析与创新
随着深度学习的发展,人机对话技术取得重大突破,成为人工智能领域的热点研究问题. 人机对话作为人机交互系统的核心功能之一,发挥着十分重要的作用.相比其他传统交互方式,人机对话可在聊天的过程中完成输入信息 ...
- 直播 | 平安人寿资深算法工程师姚晓远:对话生成模型的探析与创新
随着深度学习的发展,人机对话技术取得重大突破,成为人工智能领域的热点研究问题. 人机对话作为人机交互系统的核心功能之一,发挥着十分重要的作用.相比其他传统交互方式,人机对话可在聊天的过程中完成输入信息 ...
- 大一新生计算机课word知识,大学新生计算机基础分层考试结果探析与启发.doc
大学新生计算机基础分层考试结果探析与启发 大学新生计算机基础分层考试结果分析及启发 摘要:对计算机分层考试的结果进行统计分析,得出的主要结论包括:取消大学计算机基础课程是不合适的:大学新生对计算机基础 ...
- 计算机基础考试试题(扩招考试),大学计算机科学基础探析论文
"大学计算机基础"是各所高校非常重要的一门课程,其理论课程以讲解计算机基础知识为主,而将工具性.操作性的内容放到实验课中.下面是学习啦小编给大家推荐的大学计算机科学基础探析论文,希 ...
最新文章
- c#获取电脑硬件信息参数说明(主板篇 Win32_BaseBoard )
- linux中权限的修改
- 空指针错误 java.lang.NullPointerException 浅谈
- 4.9 总结-深度学习第一课《神经网络与深度学习》-Stanford吴恩达教授
- pyqt5教程12:拖放功能
- 官方首次披露,TDSQL十年自主可控之路(附PDF)
- 今天学习啦所谓的高级语言啦
- maven 打包编译_您是否真的想加快Maven的编译/打包速度? 那么takari生命周期插件就是答案。...
- nginx css 304 导致图片丢失_Nginx面试三连问:如何工作?负载均衡策略有哪些?如何限流?...
- C#调用windows API实现 smallpdf客户端程序进行批量压缩
- 如何清理浏览器缓存快捷键
- 18-(基础入门篇)GPRS(Air202)拨打电话
- 免费开源的拉曼光谱分析软件 Raman Spectral Analysis software
- (一)基于Django的人脸识别在线考试系统
- mysql导入错误1148,将CSV导入MySQL表会返回错误#1148
- 基于java的简单的(即时通讯)聊天程序
- 塔式太阳能热发电系统的防雷设计
- 安装配置apache
- 【mysql】You must reset your password using ALTER USER statement before executing this statement报错处理
- C++学习(三十九)(C语言部分)之 游戏项目(2048游戏)