基本概念
1、操作(Operation):在 NSOperation 中,我们使用 NSOperation 子类 NSInvocationOperation、NSBlockOperation,或者自定义子类来封装操作
maxConcurrentOperationCount是最大并发数。在队列中同时执行任务的个数不是线程的个数,线程的个数取决于系统,由系统分配
2、maxConcurrentOperationCount 默认情况下为-1,表示不进行限制,可进行并发执行
maxConcurrentOperationCount 为1时,队列为串行队列。
maxConcurrentOperationCount 大于1时,队列为并发队列.当然这个值不应超过系统限制,即使自己设置一个很大的值,系统也会自动调整为 min{自己设定的值,系统设定的默认最大值}。(同时执行的任务数)
3、NSOperationQueue基本使用
(1)NSOperation中的两种队列
主队列 通过mainQueue创建,凡是放到主队列中的
任务都将在主线程执行
(2)非主队列
alloc init 创建,同时具备了并发和串行的功能
通过设置最大并发数属性来控制任务是并发执行还
是串行执行
(3)创建相关代码
自定义NSOperation
-(void)customOperation
{
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封装操作
//好处:1.信息隐蔽
//2.代码复用
XMGOperation *op1 = [[XMGOperation alloc]init];
XMGOperation *op2 = [[XMGOperation alloc]init];
//3.添加操作到队列中
[queue addOperation:op1];
[queue addOperation:op2];
}
NSBlockOperation
- (void)block
{
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封装操作
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1----%@",[NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2----%@",[NSThread currentThread]);
}];
// 给任务op2追加任务
[op2 addExecutionBlock:^{
NSLog(@"3----%@",[NSThread currentThread]);
}];
[op2 addExecutionBlock:^{
NSLog(@"4----%@",[NSThread currentThread]);
}];
//3.添加操作到队列中
[queue addOperation:op1];
[queue addOperation:op2];
//补充:简便方法 直接添加任务
[queue addOperationWithBlock:^{
NSLog(@"5----%@",[NSThread currentThread]);
}];
}
NSInvocationOperation
- (void)invocation
{
/*
GCD中的队列:
串行队列:自己创建的,主队列
并发队列:自己创建的,全局并发队列
NSOperationQueue
主队列:[NSOperationQueue mainqueue];凡事放在主队列中的操作都在主线程中执行
非主队列:[[NSOperationQueue alloc]init],并发和串行,默认是并发执行的
*/
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封装操作
NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];
NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];
NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil];
//3.把封装好的操作添加到队列中
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
}
NSOperation其它用法
NSOperation是执行操作的意思,换句话说就是你在线程中执行的那段代码,在 GCD 中是放在 block 中的。
在 NSOperation 中,使用 NSOperation 子类 NSInvocationOperation、NSBlockOperation,或者自定义子类来封装操作。
暂停和恢复以及取消
设置暂停和恢复
suspended设置为YES表示暂停,suspended设置为NO表示恢复
暂停表示不继续执行队列中的下一个任务,暂停操作是可以恢复的
if (self.queue.isSuspended) {
self.queue.suspended = NO;
}else
{
self.queue.suspended = YES;
}
取消队列里面的所有操作
取消之后,当前正在执行的操作的下一个操作将不再执行,而且永远都不在执行,就像后面的所有任务都从队列里面移除了一样
取消操作是不可以恢复的
[self.queue cancelAllOperations];
自定义NSOperation取消操作
自定义NSOperation 重写main方法
-(void)main
{
//耗时操作1
for (int i = 0; i<1000; i++) {
NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);
}
NSLog(@"+++++++++++++++++++++++++++++++++");
//苹果官方建议,每当执行完一次耗时操作之后,就查看一下当前队列是否为取消状态,如果是,那么就直接退出
//好处是可以提高程序的性能
if (self.isCancelled) {
return;
}
//耗时操作2
for (int i = 0; i<1000; i++) {
NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);
}
NSLog(@"+++++++++++++++++++++++++++++++++");
}
附上代码demo
NSOperationQueue的优点
NSOperation、NSOperationQueue 是苹果提供给我们的一套多线程解决方案。实际上 NSOperation、NSOperationQueue 是基于 GCD 更高一层的封装,完全面向对象。但是比 GCD 更简单易用、代码可读性也更高。
- 可以添加任务依赖,方便控制执行顺序
- 可以设定操作执行的优先级
- 任务执行状态控制:isReady,isExecuting,isFinished,isCancelled
如果只是重写NSOperation的main方法,由底层控制变更任务执行及完成状态,以及任务退出。
如果重写了NSOperation的start方法,自行控制任务状态
系统通过KVO的方式移除isFinished==YES的NSOperation - 可以设置最大并发数
网友评论