iOS多线程总结2-NSOperation

iOS多线程总结2-NSOperation

欢迎交流,欢迎指出错误

推荐雷纯峰大大的一篇文章《iOS 并发编程之 Operation Queues

简介

NSOperation是基于GCD封装的,是一套面向对象的多线程API。

一、NSOperationQueue

NSOperationQueue是操作队列,简单介绍一下它的一些方法属性。

  1. maxConcurrentOperationCount

The default maximum number of operations is determined dynamically by the NSOperationQueue object based on current system conditions.

这句话的意思是maxConcurrentOperationCount缺省值是由当前系统动态决定。

当等于1时,操作队列为串行队列。

  1. qualityOfService

服务的优先级

Used to indicate the nature and importance of work to the system. Work with higher quality of service classes receive more resources than work with lower quality of service classes whenever there is resource contention.

这句话的意思是qualityOfService的级别越高,当前队列的优先级就越高,可以获得更多的系统资源。

typedef NS_ENUM(NSInteger, NSQualityOfService) {
    NSQualityOfServiceUserInteractive = 0x21,
    NSQualityOfServiceUserInitiated = 0x19,
    NSQualityOfServiceUtility = 0x11,
    NSQualityOfServiceBackground = 0x09,

    /* Default QoS indicates the absence of QoS information.  Whenever possible QoS information will be inferred from other sources.  If such inference is not possible, a QoS between UserInitiated and Utility will be used. */
    NSQualityOfServiceDefault = -1
} NS_ENUM_AVAILABLE(10_10, 8_0);

  • NSQualityOfServiceUserInteractive:主要用于提供交互UI的交互;
  • NSQualityOfServiceUserInitiated:主要用于需要立即返回的任务;
  • NSQualityOfServiceUtility:用于不需要立即返回的任务;
  • NSQualityOfServiceBackground:用于不紧急的后台任务;
  • NSQualityOfServiceDefault:当没有设置优先级的时候,线程默认优先级,通常是根据发起者来确定的,如果没有这方面的信息,那么Default的优先级就会在UserInitiatedUtility之间;

NSOperation

具体使用就不写了,推荐文章很详细了。写一下使用时要注意的一个点。

queuePriority的设置

操作Operation在操作队列OperationQueue中的优先级

优先级最高并不一定就是第一个被队列派发执行,这和队列的最大并发数有关系。

看下面这段代码:

- (void)queueTest {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue setMaxConcurrentOperationCount:5];
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"invocation test1"];
    [operation1 setQueuePriority:NSOperationQueuePriorityLow];
    
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"invocation test2"];
    [operation2 setQueuePriority:NSOperationQueuePriorityLow];

    NSInvocationOperation *operation3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"invocation test3"];
    [operation3 setQueuePriority:NSOperationQueuePriorityVeryHigh];

    NSInvocationOperation *operation4 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"invocation test4"];
    [operation4 setQueuePriority:NSOperationQueuePriorityLow];
    NSInvocationOperation *operation5 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"invocation test5"];
    [operation5 setQueuePriority:NSOperationQueuePriorityLow];
    NSInvocationOperation *operation6 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"invocation test6"];
    [operation6 setQueuePriority:NSOperationQueuePriorityLow];
    NSInvocationOperation *operation7 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"invocation test7"];
    [operation7 setQueuePriority:NSOperationQueuePriorityLow];
    NSInvocationOperation *operation8 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test:) object:@"invocation test8"];
    [operation8 setQueuePriority:NSOperationQueuePriorityLow];
    [queue addOperations:@[operation1, operation2, operation4, operation5, operation6, operation7, operation8,operation3] waitUntilFinished:YES];
}

- (void)test:(NSString *)obj {
    NSLog(@"%@", [NSThread currentThread]);
    NSLog(@"%@", obj);
}
2017-09-20 15:16:32.818 NSOperationDemo[27381:1869650] <NSThread: 0x60000007d680>{number = 6, name = (null)}
2017-09-20 15:16:32.818 NSOperationDemo[27381:1869634] <NSThread: 0x60000007d580>{number = 5, name = (null)}
2017-09-20 15:16:32.818 NSOperationDemo[27381:1869633] <NSThread: 0x6080000789c0>{number = 4, name = (null)}
2017-09-20 15:16:32.818 NSOperationDemo[27381:1869649] <NSThread: 0x60000007d500>{number = 3, name = (null)}
2017-09-20 15:16:32.819 NSOperationDemo[27381:1869634] invocation test2
2017-09-20 15:16:32.818 NSOperationDemo[27381:1869650] invocation test4
2017-09-20 15:16:32.819 NSOperationDemo[27381:1869633] invocation test1
2017-09-20 15:16:32.819 NSOperationDemo[27381:1869636] <NSThread: 0x60000007d5c0>{number = 7, name = (null)}
2017-09-20 15:16:32.819 NSOperationDemo[27381:1869649] invocation test3
2017-09-20 15:16:32.820 NSOperationDemo[27381:1869650] <NSThread: 0x60000007d680>{number = 6, name = (null)}
2017-09-20 15:16:32.820 NSOperationDemo[27381:1869634] <NSThread: 0x60000007d580>{number = 5, name = (null)}
2017-09-20 15:16:32.820 NSOperationDemo[27381:1869636] invocation test5
2017-09-20 15:16:32.820 NSOperationDemo[27381:1869633] <NSThread: 0x6080000789c0>{number = 4, name = (null)}
2017-09-20 15:16:32.820 NSOperationDemo[27381:1869650] invocation test6
2017-09-20 15:16:32.820 NSOperationDemo[27381:1869634] invocation test7
2017-09-20 15:16:32.866 NSOperationDemo[27381:1869633] invocation test8

当并发数为5时,通过log打印,我们可以发现优先级最高的operation3并不是第一个派发的。我们将maxConcurrentOperationCount改为2,再看一下log:

2017-09-20 15:21:59.866 NSOperationDemo[27497:1879269] <NSThread: 0x60000007bb80>{number = 3, name = (null)}
2017-09-20 15:21:59.866 NSOperationDemo[27497:1879268] <NSThread: 0x600000262d00>{number = 4, name = (null)}
2017-09-20 15:21:59.867 NSOperationDemo[27497:1879269] invocation test3
2017-09-20 15:21:59.867 NSOperationDemo[27497:1879268] invocation test1
2017-09-20 15:21:59.868 NSOperationDemo[27497:1879269] <NSThread: 0x60000007bb80>{number = 3, name = (null)}
2017-09-20 15:21:59.869 NSOperationDemo[27497:1879268] <NSThread: 0x600000262d00>{number = 4, name = (null)}
2017-09-20 15:21:59.869 NSOperationDemo[27497:1879269] invocation test2
2017-09-20 15:21:59.869 NSOperationDemo[27497:1879268] invocation test4
2017-09-20 15:21:59.872 NSOperationDemo[27497:1879282] <NSThread: 0x600000262dc0>{number = 5, name = (null)}
2017-09-20 15:21:59.872 NSOperationDemo[27497:1879268] <NSThread: 0x600000262d00>{number = 4, name = (null)}
2017-09-20 15:21:59.873 NSOperationDemo[27497:1879268] invocation test6
2017-09-20 15:21:59.873 NSOperationDemo[27497:1879282] invocation test5
2017-09-20 15:21:59.874 NSOperationDemo[27497:1879268] <NSThread: 0x600000262d00>{number = 4, name = (null)}
2017-09-20 15:21:59.874 NSOperationDemo[27497:1879282] <NSThread: 0x600000262dc0>{number = 5, name = (null)}
2017-09-20 15:21:59.895 NSOperationDemo[27497:1879268] invocation test7
2017-09-20 15:21:59.895 NSOperationDemo[27497:1879282] invocation test8

我们发现operation3是一个被派发的了。然后我们将maxConcurrentOperationCount改为3,再看一下log:

2017-09-20 15:23:44.277 NSOperationDemo[27542:1883616] <NSThread: 0x608000071f00>{number = 5, name = (null)}
2017-09-20 15:23:44.277 NSOperationDemo[27542:1883614] <NSThread: 0x60000006ae00>{number = 3, name = (null)}
2017-09-20 15:23:44.277 NSOperationDemo[27542:1883630] <NSThread: 0x608000071e40>{number = 4, name = (null)}
2017-09-20 15:23:44.278 NSOperationDemo[27542:1883616] invocation test2
2017-09-20 15:23:44.278 NSOperationDemo[27542:1883630] invocation test3
2017-09-20 15:23:44.278 NSOperationDemo[27542:1883614] invocation test1
2017-09-20 15:23:44.279 NSOperationDemo[27542:1883631] <NSThread: 0x600000070640>{number = 6, name = (null)}
2017-09-20 15:23:44.279 NSOperationDemo[27542:1883630] <NSThread: 0x608000071e40>{number = 4, name = (null)}
2017-09-20 15:23:44.279 NSOperationDemo[27542:1883616] <NSThread: 0x608000071f00>{number = 5, name = (null)}
2017-09-20 15:23:44.279 NSOperationDemo[27542:1883631] invocation test4
2017-09-20 15:23:44.280 NSOperationDemo[27542:1883630] invocation test5
2017-09-20 15:23:44.280 NSOperationDemo[27542:1883616] invocation test6
2017-09-20 15:23:44.280 NSOperationDemo[27542:1883631] <NSThread: 0x600000070640>{number = 6, name = (null)}
2017-09-20 15:23:44.280 NSOperationDemo[27542:1883614] <NSThread: 0x60000006ae00>{number = 3, name = (null)}
2017-09-20 15:23:44.317 NSOperationDemo[27542:1883631] invocation test7
2017-09-20 15:23:44.317 NSOperationDemo[27542:1883614] invocation test8

我们发现operation3又不是第一个被派发的了。

总结:优先级高低并不能决定执行顺序,他所决定的是被操作队列的派发的顺序,假如操作队列的并发数是2,那么操作队列第一次派发的2个任务中,一定有优先级最高的任务,至于在本次派发,该任务第几个被执行,则是不确定的。

三、NSBlockOperation

推荐文章讲的很好了,不写了。

四、自定义NSOperation子类

推荐文章讲的很好了,不写了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容