IOS开发—多线程之NSOperation介绍

使用NSOperation实现多线程操作


一、NSOperation实现多线程介绍

使用NSOperation实现多线程,必须配合NSOPerationQueue使用,具体步骤如下:

1、将需要执行的操作封装到NSOperation对象中。(由于NSOperation是一个抽象类,不具备封装操作的能力,必须使用它的子类:NSInvocationOperation或者NSBlockOperation或者自定义的继承于NSOPeration的子类);

2、将封装了具体操作的NSOperation对象添加到操作队列(NSOperationQueue)中。

3、系统会自动将NSOperationQueue中的NSOperation封装好的操作一条一条取出来,放到不同的线程中执行。


NSOperationQueue有两种不同的队列,一种是主队列,其中的操作只能在主线程中执行;另一种是自定义队列,其中的操作开辟新的线程并发执行。

NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];  //主队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];  //自定义队列
 

二、NSInvocationOperation子类和NSBlockOperation及NSOperationQUeue的使用

1、NSInvocationOperation子类和NSBlockOperation子类对象在不添加到操作队列的情况下执行操作:

#import "ViewController.h"
@interface ViewController ()
@end
 
@implementationViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self mainOperation];
}
 
- (void)mainOperation
{
    //创建NSInvocationOperation操作对象封装才做
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
    //创建NSBlockOperation操作对象封装才做
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"操作3(1)当前线程————%@",[NSThread currentThread]);
    }];
    //向NSBlockOperation操作对象中添加操作
    [operation3 addExecutionBlock:^{
        NSLog(@"操作3(2))当前线程————%@",[NSThread currentThread]);
    }];
   
    [operation3 addExecutionBlock:^{
        NSLog(@"操作3(3)当前线程————%@",[NSThread currentThread]);
    }];
   
    //执行操作
    [operation1 start];
    [operation2 start];
    [operation3 start];
}
 
- (void)test1
{
    NSLog(@"操作1当前线程————%@",[NSThread currentThread]);
}
 
- (void)test2
{
    NSLog(@"操作2当前线程————%@",[NSThread currentThread]);
}
@end


打印结果:

说明:对于NSInvocationOperation操作对象,如果没有将操作对象添加到操作队列中,其中的操作默认是在主线程中同步执行的。对于NSBlockOperation操作对象,若没有添加到操作队列中,执行其中的操作的时候开辟了新的线程实现异步执行。

 

2、子类对象添加到操作队列中:

#import "ViewController.h"
@interface ViewController ()
@end
 
@implementationViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self mainOperation];
}
 
- (void)mainOperation
{
    //创建NSInvocationOperation操作对象封装才做
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
    //创建NSBlockOperation操作对象封装才做
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"操作3(1)当前线程————%@",[NSThread currentThread]);
    }];
    //向NSBlockOperation操作对象中添加操作
    [operation3 addExecutionBlock:^{
        NSLog(@"操作3(2))当前线程————%@",[NSThread currentThread]);
    }];
   
    [operation3 addExecutionBlock:^{
        NSLog(@"操作3(3)当前线程————%@",[NSThread currentThread]);
    }];
   
    //创建操作队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
   
    //系统自动异步执行queue中的操作
   
//    //执行操作
//    [operation1 start];
//    [operation2 start];
//    [operation3 start];
}
 
- (void)test1
{
    NSLog(@"操作1当前线程————%@",[NSThread currentThread]);
}
 
- (void)test2
{
    NSLog(@"操作2当前线程————%@",[NSThread currentThread]);
}
@end

 

说明:添加到操作队列中的操作对象,系统会自动从中(按顺序)取出封装好的任务,开辟新的线程异步执行。从打印结果来看并虽然队列中任务的取出是有顺序的,但是打印结果是乱的。这就好比A、B、C三名选手同时起跑,最终到达终点并不一定是ABC这样的顺序道理是一样的。(事实上上面的代码多执行几次就可以看到打印结果是会变的)。

打印结果:



三、操作依赖关系

NSOperation之间可以设置操作依赖关系来保证执行的先后顺序:

[operation2 addDependency:operation1];

代码示例:

#import "ViewController.h"
@interface ViewController ()
@end
 
@implementationViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self mainOperation];
}
 
- (void)mainOperation
{
    //创建NSInvocationOperation操作对象封装才做
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
    //创建NSBlockOperation操作对象封装才做
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"操作3(1)当前线程————%@",[NSThread currentThread]);
    }];
    //向NSBlockOperation操作对象中添加操作
    [operation3 addExecutionBlock:^{
        NSLog(@"操作3(2))当前线程————%@",[NSThread currentThread]);
    }];
   
    [operation3 addExecutionBlock:^{
        NSLog(@"操作3(3)当前线程————%@",[NSThread currentThread]);
    }];
   
    //设置操作的先后依赖关系
    [operation2 addDependency:operation3];
    [operation3 addDependency:operation1];
   
    //创建操作队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
   
    //系统自动异步执行queue中的操作
   
//    //执行操作
//    [operation1 start];
//    [operation2 start];
//    [operation3 start];
}
 
- (void)test1
{
    NSLog(@"操作1当前线程————%@",[NSThread currentThread]);
}
 
- (void)test2
{
    NSLog(@"操作2当前线程————%@",[NSThread currentThread]);
}
@end


打印结果:操作按照1->3->2的顺序先后执行


四、操作的监听

可以监听一个操作的执行完毕:

#import "ViewController.h"
@interface ViewController ()
@end
 
@implementationViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self mainOperation];
}
 
- (void)mainOperation
{
    NSBlockOperation*operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"操作1当前线程————%@",[NSThread currentThread]);
    }];
    //监听操作1的执行完毕
    operation1.completionBlock = ^{
        NSLog(@"操作1操作结束");
    };
   
    [operation1 start];
   
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
    //监听操作2的执行完毕
    operation2.completionBlock = ^{
        NSLog(@"操作2操作结束");
    };
    [operation2 start];
}
 
- (void)test
{
    NSLog(@"操作2当前线程————%@",[NSThread currentThread]);
}
@end

打印结果:


说明:在上一个任务执行完后,会执行operation.completionBlock=^{}代码段,且是在当前线程执行。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值