多线程NSOperation简单使用

多线程NSOperation简单使用_第1张图片
detail-10.jpeg

知识树截图

多线程NSOperation简单使用_第2张图片
Snip20160727_3.png
NO.1 NSInvocationOperation封装任务
#对于NSOperation有两个子类,先看看这个NSInvocationOperation
-(void)invocationOperationWithQueue
{
    //1.创建操作,封装任务(task1,2,3就是你要调用的方法)
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task1) object:nil];
     NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task2) object:nil];
     NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(task3) object:nil];
    
   //NSOperation有两种队列
    //主队列:[NSOperationQueue mainQueue](是在主线程中执行)
    //非主队列: [[NSOperationQueue alloc]init](特殊:同时具备串行和并发的功能)
    //默认情况下非主队列是并发执行
 //2.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //3.添加任务到队列中
    [queue addOperation:op1];
     [queue addOperation:op2];
    [queue addOperation:op3];
}
#注:会开辟线程
NO.2 NSBlockOperation封装任务
-(void)blockOperationWithQueue
{
 //1.创建操作
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"丑八怪---%@",[NSThread currentThread]);
    }];
   NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
       NSLog(@"丑八怪---%@",[NSThread currentThread]);
    }];
  NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
       NSLog(@"丑八怪---%@",[NSThread currentThread]);
    }];
    //2.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
   //3.添加任务到队列中
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
#pragma mark -- 对于block的简便方法
    [queue addOperationWithBlock:^{
        NSLog(@"丑八怪---%@",[NSThread currentThread]);
   }];
 }  
#注:会开辟线程
NO.3 自定义NSOperation
1.先新建文件,继承于NSOperation
2.在控制器中的方法是(要到入头文件哦)
-(void)customWithQueue
{
   //1.封装操作
    MyOperation *op1 = [[MyOperation alloc]init];
     MyOperation *op2 = [[MyOperation alloc]init];
  //2.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
  //3.添加操作到队列中
   [queue addOperation:op1];
    [queue addOperation:op2];
 }
//3.在自定义的NSOperation中重写main方法
#import "MyOperation.h"
@implementation MyOperation
//重写main方法,告知要执行的任务是什么
-(void)main
{
  NSLog(@"main -- %@",[NSThread currentThread]);
}
@end
#提问:苹果为什么要提供这么个东东让我们可以重写main方法来执行任务
#解答:我们可以把任务单独封装起来,控制器不用关心执行什么样的任务,有多少任务需要执行
NO.4非主队列如何实现串行以及队列暂停,继续,取消
-(void)test
{
  //1.创建队列
    //默认是并发队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //2.设置最大并发数(同一时间最多有多少个任务可以执行)
    queue.maxConcurrentOperationCount = 1;
    #最大并发数设置为1,任务就是串行执行的
}
#pragma mark -- 开始执行任务按钮
- (IBAction)startBtn:(UIButton *)sender {

    //1.创建队列
    //默认是并发队列
    self.queue = [[NSOperationQueue alloc]init];
  //2.设置最大并发数(同一时间最多有多少个任务可以执行)
   self.queue.maxConcurrentOperationCount = 1;//串行队列了
//3.创建任务
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        for (NSInteger i = 0; i < 1000; i++) {
            NSLog(@"1---%zd----%@",i,[NSThread currentThread]);
        }
   }];
 NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        for (NSInteger i = 0; i < 1000; i++) {
            NSLog(@"2---%zd----%@",i,[NSThread currentThread]);
        }
   }];
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        for (NSInteger i = 0; i < 1000; i++) {
            NSLog(@"3---%zd----%@",i,[NSThread currentThread]);
        }
    }];
    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
        for (NSInteger i = 0; i < 1000; i++) {
            NSLog(@"4---%zd----%@",i,[NSThread currentThread]);
        }
     }];
 NSBlockOperation *op5 = [NSBlockOperation blockOperationWithBlock:^{
        for (NSInteger i = 0; i < 1000; i++) {
            NSLog(@"5---%zd----%@",i,[NSThread currentThread]);
        }
    }];
//4.给队列添加任务
     [self.queue addOperation:op1];
     [self.queue addOperation:op2];
     [self.queue addOperation:op3];
     [self.queue addOperation:op4];
     [self.queue addOperation:op5];
}

#pragma mark -- 暂停队列(即使点击了暂停也会把当前的任务执行完以后才会继续的)
- (IBAction)pauseBtn:(UIButton *)sender {
# setSuspended :这个属性是个BOOL值 
[self.queue setSuspended:YES];
}
#pragma mark -- 继续执行
- (IBAction)continueBtn:(UIButton *)sender {
    [self.queue setSuspended:NO];
}
#pragma mark -- 取消执行
- (IBAction)cancelBtn:(UIButton *)sender {
     [self.queue cancelAllOperations];
}

NO.5 NSOperation队列的依赖和监听
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
   //2.封装任务
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
      NSLog(@"----%@",[NSThread currentThread]);
    }];
  NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
       NSLog(@"----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
         NSLog(@"----%@",[NSThread currentThread]);
    }];
    NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
       NSLog(@"----%@",[NSThread currentThread]);
    }];
 #pragma mark -- 需求是比如我们在下载电影,这个任务中得电影下载成功后,给我一个提醒
    //操作监听
    op3.completionBlock = ^{
         NSLog(@"******客官您的电影下载好了");
    };
  #pragma mark -- 我们的需求是控制并发任务的执行顺序
    //添加操作依赖(这样写后执行顺序变为:2341)
    [op1 addDependency:op4];//1任务的依赖为4任务,1任务只有在4任务执行完后才能执行
    [op4 addDependency:op3];
    [op3 addDependency:op2];
    //3.添加任务到队列中
    [queue addOperation:op4];
     [queue addOperation:op3];
     [queue addOperation:op2];
     [queue addOperation:op1];
  }
NO.6 NSOperation线程间的通信(以下载一张图片为例)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     //1.开子线程去下载图片(队列用非主队列)
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //2.封装操作
    NSBlockOperation *downLoadImage = [NSBlockOperation blockOperationWithBlock:^{
       //获得图片地址
        NSURL *imageURL = [NSURL URLWithString: @"http://img0.imgtn.bdimg.com/it/u=2848928257,3412257867&fm=206&gp=0.jpg"];
     //将图片下载下来(是二进制数据)
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
       //将二进制数据转换为图片
        UIImage *image = [UIImage imageWithData:imageData];
       NSLog(@"%@",[NSThread currentThread]);
    //更新UI(回到主队列)
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
         self.imageView.image = image;
              NSLog(@"%@",[NSThread currentThread]);
        }];
   }];
   //3.将任务添加到队列中
    [queue addOperation:downLoadImage];
}

后记:这些都是多线程NSOperation的基础用法,至于具体运用场景,就是以实际情况为准了

你可能感兴趣的:(多线程NSOperation简单使用)