zoukankan      html  css  js  c++  java
  • iOS 多线程(NSOperationQueue)

    1.就基本使用

    NSInvocationOperation

    //1.创建操作对象,封装需要执行的任务
        NSInvocationOperation * operation =[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];
        
        //2.执行操作(默认情况下,如果操作没有放到队列queue中,都是同步执行)
        [operation start];
    
    - (void)download{
        NSLog(@"--------下载图片-----%@",[NSThread currentThread]);
    }
    

    NSBlockOperation

     //1.封装操作
        NSBlockOperation * operation=[NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"--------下载图片-----%@",[NSThread currentThread]);
        }];
        
        [operation addExecutionBlock:^{
             NSLog(@"--------下载图片1-----%@",[NSThread currentThread]);
        }];
        [operation addExecutionBlock:^{
             NSLog(@"--------下载图片2-----%@",[NSThread currentThread]);
        }];
        
    
        //2.执行操作
        [operation start];
        
        //注意:只要NSBlockOperation封装的操作数>1,就会异步执行操作
    

     NSOperationQueue 操作队列

     //创建操作队列
        NSOperationQueue * queue =[[NSOperationQueue alloc]init];
        NSInvocationOperation *operation1 =[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];
        NSInvocationOperation * operation2 =[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run) object:nil];
        
        NSBlockOperation * operation3 =[NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"--------下载图片2-----%@",[NSThread currentThread]);
            
        }];
        
        [operation3 addExecutionBlock:^{
            NSLog(@"--------run2-----%@",[NSThread currentThread]);
    
        }];
        
        
        [queue addOperation:operation1];
        [queue addOperation:operation2];
        [queue addOperation:operation3];
     //将操作放在队列里才能异步操作
    

    *设置最大并发数

    - (void)setMaxConcurrentOperationCount:(NSInteger )cnt;

    *设置依赖

    [opetationB  addDependency:operationA];//操作B依赖于操作A       A执行完才会执行B

  • 相关阅读:
    【Leetcode】Unique Binary Search Trees
    linux C函数之access函数的用法
    Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析
    【Leetcod】Unique Binary Search Trees II
    KVM客户机使用主机USB设备
    运行Maven是报错:No goals have been specified for this build
    SQL2008R2 express版本不支持维护计划
    已超过了锁请求超时时段的原因
    Oracle免客户端InstantClient安装使用
    将存储过程的返回值赋给变量
  • 原文地址:https://www.cnblogs.com/wangbinbin/p/4800562.html
Copyright © 2011-2022 走看看