zoukankan      html  css  js  c++  java
  • 多线程

    1. NSOperation简介

    NSOperation 是苹果提供给我们的一套多线程解决方案。实际上NSOperation是基于GCD更高一层的封装,但是比GCD更简单易用、代码可读性也更高。

    NSOperation需要配合NSOperationQueue来实现多线程。因为在默认情况下,NSOperation单独使用时系统同步执行操作,并没有开辟新线程的能力,只有NSOperationQueue才能实现异步执行。

    NSOperation是基于GCD的,那么使用起来也和GCD差不多,其中,NSOperation相当于GCD中的任务,而NSOperationQueue则相当于GCD中的队列。NSOperation实现多线程的使用步骤分为三步:

    • 创建任务:先将需要执行的操作封装到一个NSOperation对象中;
    • 创建队列:创建NSOperationQueue对象;
    • 将任务加入到队列中:然后将NSOperation对象添加到NSOperationQueue中。

    之后,系统就会自动将NSOperationQueue中的NSOperation取出来,在新县城中执行操作。

    - - - - - - - - - 下面我们来学习下NSOperation和NSOperationQueue的基本使用 - - - - - - 

    2. NSOperation的基本使用

    2.1 创建任务

    NSOperation是个抽象类,并不能封装任务。我们只有使用它的子类来封装任务,我们有三种方式来封装任务。

    a. 使用子类NSInvocationOperation

    b. 使用子类NSBlockOperation

    c. 定义继承自NSOperation的子类,通过实现内部相应的方法来封装任务。

    在不使用NSOperationQueue,单独使用NSOperation的情况下系统同步执行操作,下面我们了解一下:

    2.2 使用子类NSInvocationOperation

    // 1.创建NSInvocationOperation对象
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil];
    
    // 2.调用start方法开始执行操作
    [op start];
    
    - (void)run
    {
        NSLog(@"------%@", [NSThread currentThread]);
    }
    

     输出结果:

    <NSThread: 0x604000260b00>{number = 1, name = main}

    从中可以看出,在没有使用NSOperationQueue,单独使用NSOperation的情况下,NSInvocationOperation是在当前线程中执行操作,并没有创建新的线程。因为,当前线程是主线程,所以打印出的是主线程。

    2.3 使用NSBlockOperation

    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        // 在主线程
        NSLog(@"------%@", [NSThread currentThread]);
    }];
    
    [op start];
    

    输出结果:

    <NSThread: 0x604000260b00>{number = 1, name = main}
    

     从中可以看出,在没有使用NSOperationQueue,单独使用NSOperation的情况下,NSBlockOperation是在主线程中执行操作,并没有创建新的线程。

    但是NSBlockOperation还提供了一个方法 addExecutionBlock:,通过这个方法就可以为NSBlockOperation添加额外的操作,这些额外的操作就会在其它线程中并发执行。

    输出结果

     NSOperation[1326:87276] 3 - <NSThread: 0x6000002715c0>{number = 4, name = (null)}
     NSOperation[1326:87282] 1 - <NSThread: 0x604000271780>{number = 3, name = (null)}
     NSOperation[1326:87200] <NSThread: 0x60000006c6c0>{number = 1, name = main}
     NSOperation[1326:87274] 2 - <NSThread: 0x6000002714c0>{number = 5, name = (null)}
    

     我们可以看到,任务都是并发执行的,NSBlockOperation中的任务是在主线程中执行的,addExecutionBlock是在新线程中执行的。

    2.4 自定义继承NSOperation的子类

    WCEOperation.h

    #import <Foundation/Foundation.h>
    
    @interface WCEOperation : NSOperation
    
    @end
    

     WCEOperation.m

    #import "WCEOperation.h"
    
    @implementation WCEOperation
    // 需要执行的任务
    -(void)main{
        for (NSInteger i = 0; i < 5; i++) {
            NSLog(@"%@",[NSThread currentThread]);
        }
    }
    
    @end
    

     在使用的时候,导入这个文件

    WCEOperation *operation = [[WCEOperation alloc] init];
    [operation start];
    

     输出结果:

    2017-12-11 22:53:42.126565+0800 NSOperation[1388:90762] <NSThread: 0x60400007fe40>{number = 1, name = main}
    2017-12-11 22:53:42.126803+0800 NSOperation[1388:90762] <NSThread: 0x60400007fe40>{number = 1, name = main}
    2017-12-11 22:53:42.126946+0800 NSOperation[1388:90762] <NSThread: 0x60400007fe40>{number = 1, name = main}
    2017-12-11 22:53:42.127171+0800 NSOperation[1388:90762] <NSThread: 0x60400007fe40>{number = 1, name = main}
    2017-12-11 22:53:42.127309+0800 NSOperation[1388:90762] <NSThread: 0x60400007fe40>{number = 1, name = main}
    

     我们可以看到,在没有使用NSOperationQueue的情况下,任务都是在主线程中执行的,并没有开启新的线程。

    3. NSOperationQueue的基本使用

    和GCD中的并发队列、串行队列略有不同的是:NSOperationQueue 一共有两种队列:主队列、其他队列。其中其他队列同时包含了串行、并发功能。下边是主队列、其他队列的基本创建方法和特点。

    - 主队列:

        凡是添加到主队列中的任务(NSOperation),都会放到主线程中执行

    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    

     - 其他队列(非主队列)

        · 添加到这种队列中的任务(NSOperation),就会自动放到子线程中执行

        · 同时包含了:串行、并发功能

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    

     4. 将任务加入到队列中

    前边说了,NSOperation需要配合NSOperationQueue来实现多线程,那么我们需要将创建好的任务加入到队列中去,总共有两种方法:

    a: - (void)addOperation:(NSOperation *)op;

    需要先创建任务,再将创建好的任务加入到创建好的队列中去

    NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
        
        NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"%@ - 1",[NSThread currentThread]);
        }];
        NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"%@ - 2",[NSThread currentThread]);
        }];
        NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"%@ - 3",[NSThread currentThread]);
        }];
        NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"%@ - 4",[NSThread currentThread]);
        }];
        NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"%@ - 5",[NSThread currentThread]);
        }];
        [opQueue addOperation:op1];
        [opQueue addOperation:op2];
        [opQueue addOperation:op3];
        [opQueue addOperation:op4];
        [opQueue addOperation:op5];
    

     运行结果:

    2017-12-11 23:07:19.203418+0800 NSOperation[1468:97665] <NSThread: 0x60000046ac80>{number = 4, name = (null)} - 4
    2017-12-11 23:07:19.203429+0800 NSOperation[1468:97664] <NSThread: 0x604000463300>{number = 6, name = (null)} - 1
    2017-12-11 23:07:19.203439+0800 NSOperation[1468:97666] <NSThread: 0x60000046a980>{number = 5, name = (null)} - 2
    2017-12-11 23:07:19.203867+0800 NSOperation[1468:97665] <NSThread: 0x60000046ac80>{number = 4, name = (null)} - 5
    2017-12-11 23:07:19.204450+0800 NSOperation[1468:97667] <NSThread: 0x604000463280>{number = 3, name = (null)} - 3
    

     可以看出,NSOperation和NSOperationQueue结合后能够开启新线程,并发执行。

    b. - (void)addOperationWithBlock:(void(^)void)block;

    无需创建任务,在block中添加任务,直接将任务block加入到队列中。

    NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
        
        [opQueue addOperationWithBlock:^{
            NSLog(@"1 - - %@",[NSThread currentThread]);
        }];
        [opQueue addOperationWithBlock:^{
            NSLog(@"2 - - %@",[NSThread currentThread]);
        }];
        [opQueue addOperationWithBlock:^{
            NSLog(@"3 - - %@",[NSThread currentThread]);
        }];
        [opQueue addOperationWithBlock:^{
            NSLog(@"4 - - %@",[NSThread currentThread]);
        }];
        [opQueue addOperationWithBlock:^{
            NSLog(@"5 - - %@",[NSThread currentThread]);
        }];
    

     运行结果:

    2017-12-11 23:17:38.951881+0800 NSOperation[1510:102617] 2 - - <NSThread: 0x600000275400>{number = 4, name = (null)}
    2017-12-11 23:17:38.951884+0800 NSOperation[1510:102615] 4 - - <NSThread: 0x6040004655c0>{number = 6, name = (null)}
    2017-12-11 23:17:38.951889+0800 NSOperation[1510:102616] 1 - - <NSThread: 0x604000465540>{number = 3, name = (null)}
    2017-12-11 23:17:38.951917+0800 NSOperation[1510:102619] 3 - - <NSThread: 0x604000465580>{number = 5, name = (null)}
    2017-12-11 23:17:38.952182+0800 NSOperation[1510:102614] 5 - - <NSThread: 0x6000002754c0>{number = 7, name = (null)}
    

     我们可以看出,都是在新的线程中执行的,并且是并发执行。

    5. 控制串行执行和并行执行的关键

    NSOperationQueue创建的其他队列同时具有串行、并发功能,上面展示了并发功能,那么他的串行功能是如何实现的?

    这里就需要用到一个关键参数:maxConcurrentOperationCount - 最大并发数。也就是最大同时执行的任务数,一般2-3,大于5之后,虽然是在子线程中处理,但是cpu处理过多的子线程,也有可能影响主线程。

    • 默认情况下为 -1,表示不进行限制,默认为并发执行
    • 当maxConcurrentOperationCount为1时,进行串行执行。
    • 当maxConcurrentOperationCount大于1时,进行并发执行,当然这个值不应超过系统限制,否则即使自己设置一个很大的值,系统也会自动调整。

    还是刚才那个例子,我们只设置NSOperationQueue的最大并发数为1

     NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
        
        opQueue.maxConcurrentOperationCount = 1;
        
        [opQueue addOperationWithBlock:^{
            NSLog(@"1 - - %@",[NSThread currentThread]);
        }];
        [opQueue addOperationWithBlock:^{
            NSLog(@"2 - - %@",[NSThread currentThread]);
        }];
        [opQueue addOperationWithBlock:^{
            NSLog(@"3 - - %@",[NSThread currentThread]);
        }];
        [opQueue addOperationWithBlock:^{
            NSLog(@"4 - - %@",[NSThread currentThread]);
        }];
        [opQueue addOperationWithBlock:^{
            NSLog(@"5 - - %@",[NSThread currentThread]);
        }];
    

     输出结果:

    2017-12-11 23:22:30.824859+0800 NSOperation[1535:105237] 1 - - <NSThread: 0x600000275cc0>{number = 3, name = (null)}
    2017-12-11 23:22:30.825228+0800 NSOperation[1535:105238] 2 - - <NSThread: 0x600000275d00>{number = 4, name = (null)}
    2017-12-11 23:22:30.825614+0800 NSOperation[1535:105238] 3 - - <NSThread: 0x600000275d00>{number = 4, name = (null)}
    2017-12-11 23:22:30.826585+0800 NSOperation[1535:105250] 4 - - <NSThread: 0x6000002756c0>{number = 5, name = (null)}
    2017-12-11 23:22:30.826851+0800 NSOperation[1535:105250] 5 - - <NSThread: 0x6000002756c0>{number = 5, name = (null)}
    

     可以看出,当最大并发数为1的时候,任务是按顺序串行执行的,当最大并发数为2时,任务是并发执行的,而且开启线程数量是由系统决定的。

    6. 操作依赖

    NSOperation和NSOperationQueue最吸引认得地方是它能添加操作之间的依赖关系。比如有A和B两个操作,其中A执行完操作,B才能执行操作,那么就需要让B依赖于A:

    NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
        
        NSBlockOperation *opA = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"我是opA,我先执行 - %@",[NSThread currentThread]);
        }];
        
        NSBlockOperation *opB = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"我是opB,我后执行 - %@",[NSThread currentThread]);
        }];
        
        [opB addDependency:opA];
        
        [opQueue addOperation:opB];
        [opQueue addOperation:opA];
    

     输出结果:

    2017-12-11 23:31:23.494225+0800 NSOperation[1569:109435] 我是opA,我先执行 - <NSThread: 0x604000079ec0>{number = 3, name = (null)}
    2017-12-11 23:31:23.494688+0800 NSOperation[1569:109434] 我是opB,我后执行 - <NSThread: 0x60000046f2c0>{number = 4, name = (null)}
    

     可以看到,无论运行几次,其结果都是opA先执行,opB后执行。注意:不能opA依赖opB,同时opB依赖opA。任务执行的顺序,并不取决于添加的顺序,执行的顺序取决于依赖。

    7. 其他的方法

    // NSOperation提供的方法,可取消单个操作
    - (void)cancel;
    
    // NSOperationQueue提供的方法,可以取消队列的所有操作
    - (void)cancelAllOperations;
    
    // 可以设置任务的暂停和恢复,YES代表暂停队列,No表示恢复队列。
    - (void)setSuspended:(BOOL)b;
    
    // 判断暂停状态
    - (BOOL)isSuspended;

    // 设置操作优先级 优先级越高,被调用的几率越大
    - (NSOperationQueuePriority)queuePriority;
    - (void)setQueuePriority:(NSOperationQueuePriority)p;

     注意:

    - 这里的暂停和取消并不代表可以将当前的操作立即取消,而是当当前的操作执行完毕后不再执行新的操作。

    - 暂停和取消的区别在于:暂停操作之后还可以恢复操作,继续向下执行;而取消操作之后,所有的操作就清空了,无法再接着执行剩下的操作。

  • 相关阅读:
    5. Redis持久化
    4.Redis客户端
    3.Redis高级功能
    2.Redis五种数据结构
    1.Redis简介
    32.Mysql Cluster
    suffix ACM-ICPC 2017 Asia Qingdao
    多层BFS
    我想和你们说说java和C++___C加加
    11073 最热门的K个搜索串
  • 原文地址:https://www.cnblogs.com/chenjiangxiaoyu/p/8025348.html
Copyright © 2011-2022 走看看