国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > ios多线程 -- NSOperation 简介

ios多线程 -- NSOperation 简介

来源:程序员人生   发布时间:2016-08-11 08:48:51 阅读次数:2371次

NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤:
1)先将需要履行的操作封装到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放到1个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]); }]; }

这里写图片描述

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生