IOS NSOperation&NSOperationQueue

NSOperation与NSOperationQueue的基本理论如下:
     1.NSOperationQueue代表一个FIFO的队列,它负责管理系统提交的多个NSOperation,NSOperationQueue底层维护一个线程池,会按顺序启动线程来执行提交给该队列的NSOperation任务。
     2.NSOperation:代表一个多线程任务,NSOperation还有NSInvocationOperation、NSBlockOperation两个子类。NSOperation有两种使用方式:
         1)开发者实现NSOperation的子类,
         2)开发者直接使用NSInvocationOperation或NSBlockOperation子类。
     NSInvocationOperation与NSBlockOperation在用法上非常相似,区别是NSInvocation用于将特定对象的特定方法封装成NSOperation,而NSBlockOperation则用于将代码块封装成NSOperation.
代码才是硬道理:
//
//  ViewController.m
//  CX-NSOperationQueue
//
//  Created by ma c on 16/3/15.
//  Copyright © 2016年 xubaoaichiyu. All rights reserved.
//

/*  
 GCD -- (iOS4.0)多线程解决方案
 将任务(block)添加到队列(串行,并行(全局队列)),指定执行任务的方法。(同步,异步)
 拿到dispatch_get_main_queue,线程之间的通信
 
 NSOperation -- (iOS2.0)  (后经苹果改造)
 将操作添加到队列里
 [NSOperationQueue mainQueue]  获取主队列,将任务添加到主队列,就会在主线程中执行
 NSOperation可以设置最大并发数(用GCD处理,比较麻烦)
 可以暂停可以继续,也就是挂起操作
 取消所有的任务
 设置依赖关系
 */

#import "ViewController.h"

@interface ViewController ()
//NSOperation 队列
@property (nonatomic, strong) NSOperationQueue * queue;

@end

@implementation ViewController

-(NSOperationQueue *)queue{
    
    if (!_queue) {
        _queue = [[NSOperationQueue alloc]init];
    }
    return _queue;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //线程间通信
//    [self contact];
    //最大并发数   注意观察测试时间
    [self maxCount];
    
    //常用操作
    /*
     取消操作:- (void)cancelAllOperations;
     挂起操作:@property (getter=isSuspended) BOOL suspended;
     依赖关系:- (void)addDependency:(NSOperation *)op;
     取消依赖:- (void)removeDependency:(NSOperation *)op;
     */
    
}

-(void)maxCount{
    
    self.queue.maxConcurrentOperationCount = 2;
    
    for (NSInteger i = 0 ; i < 20; i ++) {
        
        NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
        
        [self.queue addOperation:operation];
    }
}
-(void)test{
    [NSThread sleepForTimeInterval:2];
    NSLog(@"%@",[NSThread currentThread]);
}
//线程间通信
-(void) contact{
    
    [self.queue addOperationWithBlock:^{
      
        NSLog(@"%@",[NSThread currentThread]);
        NSLog(@"One");
      
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            NSLog(@"%@",[NSThread currentThread]);
            NSLog(@"Two");
        }];
    }];
}
- (void)test3 {
    
    //NSOperation是基于GCD的,把GCD的block封装成opertion,NSOperationQueue是全局队列封装
    //将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步执行的。
    
    //创建一个操作队列
    NSOperationQueue * queue = [[NSOperationQueue alloc] init];
    
    NSInvocationOperation * op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil];
    
    NSBlockOperation * op2 = [NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"op2 - %@",[NSThread currentThread]);
    }];
    
    //添加NSOperation到NSOperationQueue中
    //NSOperation添加到queue之后,通常短时间内就会得到运行。
    //    [queue addOperation:op];
    //    [queue addOperation:op2];
    
    //    waitUntilFinished yes 操作完成后执行下面的代码 no 先执行下面的代码
    
    //添加一个block形式的operation
    [queue addOperationWithBlock:^{
        
        NSLog(@"op3 - %@",[NSThread currentThread]);
        
    }];
    
    [queue addOperations:@[op,op2] waitUntilFinished:NO];
    
    //    NSLog(@"完成");
    
}

- (void)test2 {
    
    //  能够并发地执行一个或多个block对象,所有相关的block都执行完之后,操作才算完成
    NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
        
        NSLog(@"第一个操作");
    }];
    
    // 通过addExecutionBlock方法添加block操作,开启多个线程
    [op addExecutionBlock:^{
        
        NSLog(@"%@",[NSThread currentThread]);
        
        NSLog(@"第二个操作");
    }];
    
    [op addExecutionBlock:^{
        
        NSLog(@"%@",[NSThread currentThread]);
        
        NSLog(@"第三个操作");
    }];
    
    [op start];
}

- (void)test1 {
    
    // 基于一个对象和selector来创建操作。如果你已经有现有的方法来执行需要的任务,就可以使用这个类
    
    NSInvocationOperation * op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil];
    
    // 如果我们想在一个NSOperation执行完毕后做一些事情,就调用NSOperation的setCompletionBlock方法来设置想做的事情
    [op setCompletionBlock:^{
        NSLog(@"完成");
    }];
    
    // 开始执行任务(同步执行)
    // 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的。
    [op start];
    
    
}
@end

 *有关进程与线程我会进行总结*

*由于昨天网络出现问题,昨晚把代码写出来了,但是没有上传😢*

 
posted @ 2016-03-16 08:01  旭宝爱吃鱼  阅读(814)  评论(0编辑  收藏  举报