码迷,mamicode.com
首页 > 移动开发 > 详细

ios多线程 -- NSOperation 简介

时间:2016-06-28 13:06:28      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

NSOperation的作?:配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤:
1)先将需要执行的操作封装到一个NSOperation对象中
2)然后将NSOperation对象添加到NSOperationQueue中
3)系统会?动将NSOperationQueue中的NSOperation取出来
4)将取出的NSOperation封装的操作放到?条新线程中执?

NSOperation是个抽象类,并不具备封装操作的能力,必须使?它的子类
使用NSOperation?类的方式有3种:
1)NSInvocationOperation
2)NSBlockOperation
3)自定义子类继承NSOperation,实现内部相应的?法

NSInvocationOperation 开启任务方法

//用 NSInvocationOperation 创建线程
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download:) object:@"NSInvocationOperation"];
//在当前线程中同步执行
[invocation start];

- (void)download:(NSString *)url{
    NSLog(@"%@ --- %@", [NSThread currentThread], url);
}

技术分享

将创建的任务添加到队列中,异步执行

- (void)invocationOperation{
    //用 NSInvocationOperation 创建线程
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download:) object:@"NSInvocationOperation"];  
    //创建全局队列(实现异步并发执行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //从对列中取出任务执行(异步)
    [queue addOperation:invocation];

}

- (void)download:(NSString *)url{
    NSLog(@"%@ --- %@", [NSThread currentThread], url);
}

技术分享
注意:操作对象默认在主线程中执行,只有添加到队列中才会开启新的线程。即默认情况下,如果操作没有放到队列中queue中,都是同步执行。只有将NSOperation放到一个NSOperationQueue中,才会异步执行操作

NSBlockOperation 开启任务的方法

NSBlockOperation 添加任务
- (void)blockOperation2{
    //创建任务
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片1 --- %@", [NSThread currentThread]);
    }];

    [operation1 start];
}

技术分享

注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作

- (void)blockOperation2{
    //注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作
    //创建任务
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片1 --- %@", [NSThread currentThread]);
    }];

    [operation1 addExecutionBlock:^{
        NSLog(@"下载图片2 --- %@", [NSThread currentThread]);
    }];

    [operation1 addExecutionBlock:^{
        NSLog(@"下载图片3 --- %@", [NSThread currentThread]);
    }];

    [operation1 start];
}

技术分享

NSOperationQueue

NSOperationQueue的作?:NSOperation可以调?start?法来执?任务,但默认是同步执行的
如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作
添加操作到NSOperationQueue中,自动执行操作,自动开启线程
获取 NSOperationQueue 队列:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];//并发队列
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];//主队列

添加任务有两个方法:

- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block; 

实例代码

- (void)blockOperation{
    //创建全局队列(实现异步并发执行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];//并发队列
    //NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];//主队列

    //创建任务
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片1 --- %@", [NSThread currentThread]);
    }];

    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片2 --- %@", [NSThread currentThread]);
    }];

    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片3 --- %@", [NSThread currentThread]);
    }];

    NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片4 --- %@", [NSThread currentThread]);
    }];

    NSBlockOperation *operation5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片5 --- %@", [NSThread currentThread]);
    }];

    //将任务添加到队列中
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    [queue addOperation:operation4];
    [queue addOperation:operation5];
}

技术分享

- (void)queueAddBlock{
    //创建全局队列(实现异步并发执行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    //直接向队列中添加任务
    [queue addOperationWithBlock:^{
         NSLog(@"下载图片1 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下载图片2 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下载图片3 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下载图片4 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下载图片5 --- %@", [NSThread currentThread]);
    }];
}

技术分享

ios多线程 -- NSOperation 简介

标签:

原文地址:http://blog.csdn.net/alpaca12/article/details/51771781

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!