美文网首页
NSOperation介绍

NSOperation介绍

作者: 猿人 | 来源:发表于2019-12-09 13:24 被阅读0次

    NSOperation

    简介

    NSOperation 是基于GCD的一个抽象基类 ,将线程封装成要执行的操作,不需要管理线程的生命周期和同步,但比GCD可空性更强,例如可以加入操作依赖(addDependency)控制操作执行顺序,设置操作队列最大可并发执行的操作个数(setMaxConcurrentOperationCount),取消操作(cancel)等。

    NSOperation作为抽象基类不具备封装开发者的操作的功能,需要使用两个它的实体子类:NSBlockOperarion和NSInvocationOperation,或者继承NSOperation自定义子类。

    后者执行代码块,相对来说后者h更加灵活易用。NSOperation操作配置完成后便可调用start 函数在当前线程执行,如果要异步执行避免阻塞当前线程,那么可以加入 NSOperationQueue中异步执行。

    简单使用

    NSInvocationOperation

    NSInvocationOperation * invoOpertion = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run) object:nil];
    
    [invoOpertion start];
    
    

    NSBlockOperation

    NSBlockOperation * blkOperation = [NSBlockOperation blockOperationWithBlock:^{
             
     }];
     
     [blkOperation start];
    

    NSBlockOperation可以追加方法 并行执行代码块

      //线程并发队列
        NSBlockOperation * blkOperation = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"blkOperation1==%@",[NSThread currentThread]);
        }];
        
        [blkOperation addExecutionBlock:^{
            NSLog(@"blkOperation2==%@",[NSThread currentThread]);
    
        }];
        
        [blkOperation addExecutionBlock:^{
            NSLog(@"blkOperation3==%@",[NSThread currentThread]);
    
        }];
        
        [blkOperation addExecutionBlock:^{
            NSLog(@"blkOperation4==%@",[NSThread currentThread]);
    
        }];
    

    打印

    2019-12-09 12:51:42.347499+0800 NSOperation[18718:3909369] blkOperation1==<NSThread: 0x28370a600>{number = 1, name = main}
    2019-12-09 12:51:42.347517+0800 NSOperation[18718:3909551] blkOperation2==<NSThread: 0x283754480>{number = 5, name = (null)}
    2019-12-09 12:51:42.347611+0800 NSOperation[18718:3909548] blkOperation4==<NSThread: 0x283774400>{number = 6, name = (null)}
    2019-12-09 12:51:42.347611+0800 NSOperation[18718:3909549] blkOperation3==<NSThread: 0x283768c80>{number = 4, name = (null)}
    

    NSOperation 实现同步 及设置线程依赖

    //    //获取主队列
        NSOperationQueue * queue = [NSOperationQueue mainQueue];
        
        NSOperation* c = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"c");
        }];
        
        NSOperation * a = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"a");
        }];
       
        
        NSOperation * b = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"b");
        }];
        
       
        ///添加依赖 c依赖于a和b,这样c一定会在a和b完成之后才会执行
        
        [c addDependency:a];
        
        [c addDependency:b];
        
        ///添加到队列 (添加顺序故意不安顺序)
        [queue addOperation:c];
        [queue addOperation:a];
        [queue addOperation:b];
    

    打印 可以设置各种操作之间的依赖实现线程同步

    2019-12-09 13:20:53.192859+0800 NSOperation[18782:3923375] a
    2019-12-09 13:20:53.194242+0800 NSOperation[18782:3923375] b
    2019-12-09 13:20:53.200412+0800 NSOperation[18782:3923375] c
    
    

    相关文章

      网友评论

          本文标题:NSOperation介绍

          本文链接:https://www.haomeiwen.com/subject/mgubgctx.html