iOS多线程总结2-NSOperation
欢迎交流,欢迎指出错误
推荐雷纯峰大大的一篇文章《iOS 并发编程之 Operation Queues
》
简介
NSOperation是基于GCD封装的,是一套面向对象的多线程API。
一、NSOperationQueue
NSOperationQueue是
操作队列,简单介绍一下它的一些方法属性。
- maxConcurrentOperationCount
The default maximum number of operations is determined dynamically by the NSOperationQueue object based on current system conditions.
这句话的意思是maxConcurrentOperationCount缺省值是由当前系统动态决定。
当等于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
的优先级就会在UserInitiated
和Utility
之间;
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子类
推荐文章讲的很好了,不写了。
网友评论