多线程之NSOperation

简介

    NSOperation是一个基于OC实现的抽象类,以面向对象的方式封装了需要执行的操作,不必关心线程管理、同步等问题。
    NSOperation有两个子类:NSInvocationOperationNSBlockOperation。我们也可以根据自己的需要自定义NSOperation子类。
    NSOperation默认按同步方式执行,在我们调用start方法的那个线程执行。
    通常我们添加到NSOperationQueue中去执行(异步执行队列)。

创建


1. NSInvocationOperation

    //InvocationOperation 实例化
    NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(operationClick_1:)  object:nil];
    //优先级
    operation.queuePriority = NSOperationQueuePriorityNormal;

    /**
     *      NSOperationQueuePriorityVeryLow = -8L,
     *      NSOperationQueuePriorityLow = -4L,
     *      NSOperationQueuePriorityNormal = 0,
     *      NSOperationQueuePriorityHigh = 4,
     *      NSOperationQueuePriorityVeryHigh = 8

     */
    //开始
    [operation start];

2. NSBlockOperation


    //NSBlockOperation 实例化
    NSBlockOperation * blockOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"\nThis is a blockOperation :%@",[NSThread currentThread]);
    }];
    //开始
    [blockOperation start];

通过addExecutionBlock方法添加block操作
    //添加线程
    [blockOperation addExecutionBlock:^{
        NSLog(@"\nThis is add Execution block %@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
        NSLog(@"\nThis is add Execution block %@",[NSThread currentThread]);
    }];
    [blockOperation addExecutionBlock:^{
        NSLog(@"\nThis is add Execution block %@",[NSThread currentThread]);
    }];
    //并发,在不同线程中执行

3.NSOperationQueue

    //NSOperationQueue创建
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
    //创建一个invocationOperation
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(operationClick_2) object:nil];
    //创建一个blockOperation
    NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"\nThis is a blockOperation 2 :%@",[NSThread currentThread]);
    }];//因为闲的蛋疼,所以创建两个不一样的。请自便

添加Operation
    //添加Operation
    [queue addOperation:invocationOperation];
    //添加一组operation
    [queue addOperations:@[blockOperation2] waitUntilFinished:NO];
    //以block形式添加Operation
    [queue addOperationWithBlock:^{
        NSLog(@"\nThis is  an addOperation block");
    }];
    //最大并发数量 队列中最多同时运行几条线程
    queue.maxConcurrentOperationCount = 3;

依赖
    //创建一个
    NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"\nThis is a blockOperation 2 :%@",[NSThread currentThread]);
    }];
    //再创建一个
    NSBlockOperation *blockOperation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"\nThis is a blockOperation 3 :%@",[NSThread currentThread]);
    }];
    //创建队列
    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
    //添加
    [queue addOperations:@[blockOperation2,blockOperation3] waitUntilFinished:NO];

当blocOperation3的执行需要依赖blockOperation2的执行完成时,
就可以通过addDependency方法添加依赖,
blockOperation3 会在 blockOperation2执行完成后执行
当然可以添加多个依赖,但不能创建环形依赖,比如A依赖B,B依赖A,这是错误的

    [blockOperation3 addDependency:blockOperation2];
//添加依赖前
2016-04-14 16:06:30.309 NSOperationDemo[3311:259259] 
This is a blockOperation 3 :{number = 3, name = (null)}
2016-04-14 16:06:30.309 NSOperationDemo[3311:259252] 
This is a blockOperation 2 :{number = 2, name = (null)}
//添加依赖后
2016-04-14 15:58:06.076 NSOperationDemo[3271:253326] 
This is a blockOperation 2 :{number = 2, name = (null)}
2016-04-14 15:58:06.077 NSOperationDemo[3271:253326] 
This is a blockOperation 3 :{number = 2, name = (null)}
取消

一旦添加到operation queue,queue就拥有了这个Operation对象并且不能被删除,唯一能做的事情是取消。

    //取消
    [operation cancel];
    //取消所有线程
    [queue cancelAllOperations];

等待
    //如果需要在当前线程中处理operation完成后的结果
    //可以使用NSOperation的waitUntilFinished方法阻塞当前线程,等待operation完成。
    //阻塞线程
    [operation waitUntilFinished];
    //阻塞所有线程
    [queue waitUntilAllOperationsAreFinished];

暂停
    //暂停
    [queue setSuspended:YES];
    //取消暂停
    [queue setSuspended:NO];

你可能感兴趣的:(多线程之NSOperation)