https://blog.csdn.net/crycheng/article/details/21799611
本篇文章主要介绍下多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用,列举几个简单的例子。
默认情况下,NSOperation并不具备封装操作的能力,必须使用它的子类,使用NSOperation子类的方式有3种:
1> 自定义子类继承NSOperation,实现内部相应的方法
2> NSBlockOperation
3>NSInvocationOperation
这讲先介绍如何用NSOperation封装一个操作,后面再结合NSOperationQueue来使用。
1.首先介绍自定义 NSOperation:
NSOperation是没法直接使用的,它只是提供了一个工作的基本逻辑,具体实现还是需要你通过定义自己的NSOperation子类来获得。
#import <Foundation/Foundation.h>
@protocol NSDefineOprationDelegate <NSObject>
- (void) handleDelegate;
@end
@interface NSDefineOpration : NSOperation
@property (nonatomic, assign) id <NSDefineOprationDelegate> delegate;
- (id)initWithDelegate:(id<NSDefineOprationDelegate>) delegate;
@end
实现文件里:
#import "NSDefineOpration.h"
@implementation NSDefineOpration
- (id)initWithDelegate:(id<NSDefineOprationDelegate>) delegate
{
if(self = [super init])
{
self.delegate = delegate;
}
return self;
}
- (void)main
{
@autoreleasepool {
//do something
sleep(15);
NSLog(@"op1........handle...... on thread num :%@",[NSThread currentThread]);
if([self.delegate respondsToSelector:@selector(handleDelegate)])
{
[self.delegate performSelector:@selector(handleDelegate) withObject:nil];
}
}
}
@end
这里的sleep(15)主要用来做一些延时的操作,比如网络下载等。
调用:
- (void)oprationTest
{
NSDefineOpration *op1 = [[NSDefineOpration alloc] initWithDelegate:self];
op1.completionBlock = ^(){
NSLog(@"op1........OK !!");
};
[op1 start];
}
执行结果:
2014-03-22 16:49:44.367 Operation[17353:60b] op1........handle...... on thread num :<NSThread: 0x1654ea20>{name = (null), num = 1}
2014-03-22 16:49:49.373 Operation[17353:60b] op1.....callback..... on thread num :<NSThread: 0x1654ea20>{name = (null), num = 1}
2014-03-22 16:49:49.377 Operation[17353:1807] op1........OK !!
从执行结果可以看出,因为在实现的main函数里没有使用异步线程处理,导致直接阻塞了主线程1,所以使用这种方式一定注意main函数里操作时间过长导致主线程阻塞问题。耗时比较长的都放到其他线程里处理。
2.接下来介绍NSBlockOperation
第一种使用NSBlockOperation的方式:
NSLog(@"block start");
NSBlockOperation *bop2 = [NSBlockOperation blockOperationWithBlock:^{
sleep(15);
NSLog(@"bop2.....handle..... on thread num%@",[NSThread currentThread]);
}];
[bop2 setCompletionBlock:^{
NSLog(@"bop2........OK !!");
}];
[bop2 start];
* 第2行初始化了一个NSBlockOperation对象,它是用一个Block来封装需要执行的操作
* 第9行调用了start方法,紧接着会马上执行Block中的内容
看下执行结果:
2014-03-22 16:57:51.903 Operation[17374:60b] block start
2014-03-22 16:58:06.908 Operation[17374:60b] bop2.....handle..... on thread num<NSThread: 0x16d83910>{name = (null), num = 1}
2014-03-22 16:58:06.912 Operation[17374:360b] bop2........OK !!
* 这里还是在当前线程同步执行操作,并没有异步执行,阻塞主线程。
第二种使用NSBlockOperation的方式:
- (void)blockOprationTest
{
NSLog(@"block start");
NSBlockOperation * op2 = [[NSBlockOperation alloc] init];
[op2 addExecutionBlock:^{
sleep(10);
NSLog(@"op2.....handle..... on 10 hread num%@",[NSThread currentThread]);
}];
[op2 addExecutionBlock:^{
sleep(6);
NSLog(@"op2.....handle..... on 6 thread num%@",[NSThread currentThread]);
}];
[op2 addExecutionBlock:^{
sleep(4);
NSLog(@"op2.....handle..... on 4 thread num%@",[NSThread currentThread]);
}];
[op2 addExecutionBlock:^{
sleep(8);
NSLog(@"op2.....handle..... on 8 thread num%@",[NSThread currentThread]);
}];
[op2 addExecutionBlock:^{
sleep(1);
NSLog(@"op2.....handle..... on 1 thread num%@",[NSThread currentThread]);
}];
[op2 setCompletionBlock:^{
NSLog(@"op2........OK !!");
}];
[op2 start];
//bop2
NSBlockOperation *bop2 = [NSBlockOperation blockOperationWithBlock:^{
sleep(15);
NSLog(@"bop2.....handle..... on thread num%@",[NSThread currentThread]);
}];
[bop2 setCompletionBlock:^{
NSLog(@"bop2........OK !!");
}];
[bop2 start];
}
执行结果:
2014-03-22 17:02:56.027 Operation[17396:60b] block start
2014-03-22 17:03:02.032 Operation[17396:1803] op2.....handle..... on 6 thread num<NSThread: 0x17573400>{name = (null), num = 2}
2014-03-22 17:03:06.032 Operation[17396:60b] op2.....handle..... on 10 hread num<NSThread: 0x1755eca0>{name = (null), num = 1}
2014-03-22 17:03:06.035 Operation[17396:1803] op2.....handle..... on 4 thread num<NSThread: 0x17573400>{name = (null), num = 2}
2014-03-22 17:03:07.038 Operation[17396:1803] op2.....handle..... on 1 thread num<NSThread: 0x17573400>{name = (null), num = 2}
2014-03-22 17:03:14.035 Operation[17396:60b] op2.....handle..... on 8 thread num<NSThread: 0x1755eca0>{name = (null), num = 1}
2014-03-22 17:03:14.037 Operation[17396:1807] op2........OK !!
2014-03-22 17:03:29.038 Operation[17396:60b] bop2.....handle..... on thread num<NSThread: 0x1755eca0>{name = (null), num = 1}
2014-03-22 17:03:29.041 Operation[17396:180b] bop2........OK !!
分析下结果:
首先看到了有1和2两个线程,线程2在56秒的时候开始执行6秒的操作,接下来执行4,1秒结束时间为07秒。线程1在56的时候开始执行10秒的操作,接下来执行8秒,结束时间为14秒。最后执行Bop2的15秒操作至29秒。时间看起来没有问题。为什么会启用2个线程而不是3个或者更多?
3.接下来介绍NSInvocationOperation
- (void)invocationOperation
{
NSInvocationOperation * op3 = [[NSInvocationOperation alloc] initWithTarget:(id)self selector:@selector(handleInvoOpDelegate) object:nil];
[op3 setCompletionBlock:^{
NSLog(@"op3........OK !!");
}];
[op3 start];
}
selector函数:
- (void)handleInvoOpD
{
sleep(5);
NSLog(@"op3.....handle..... on thread num :%@",[NSThread currentThread]);
}
执行结果:
2014-03-22 17:11:13.050 Operation[17421:60b] op3.....handle..... on thread num :<NSThread: 0x1653ac30>{name = (null), num = 1}
2014-03-22 17:11:13.055 Operation[17421:1803] op3........OK !!
NSInvocationOperation比较简单,就是继承了NSOperation,区别就是 它是基于一个对象和selector来创建操作,可以直接使用而不需继承来实现自己的操作处理。
4.最后介绍下NSOperationQueue
把NSOperation子类的对象放入NSOperationQueue队列中,该队列就会启动并开始处理它。队列里可以加入很多个NSOperation, 可以把NSOperationQueue看作一个线程池,可往线程池中添加操作(NSOperation)到队列中。线程池中的线程可看作消费者,从队列中取走操作,并执行它。
实现demo:
- (void)handleOpqueue
{
NSOperationQueue *qu = [[NSOperationQueue alloc] init];
NSBlockOperation * bkOp1 = [NSBlockOperation blockOperationWithBlock:^{
sleep(10);
NSLog(@"bkOp1.....handle.....on thread num%@",[NSThread currentThread]);
}];
[bkOp1 setCompletionBlock:^{
NSLog(@"bkOp1........OK !!");
}];
NSBlockOperation * bkOp2 = [NSBlockOperation blockOperationWithBlock:^{
sleep(2);
NSLog(@"bkOp2.....handle.....on thread num%@",[NSThread currentThread]);
}];
[bkOp2 setCompletionBlock:^{
NSLog(@"bkOp2........OK !!");
}];
NSBlockOperation * bkOp3 = [NSBlockOperation blockOperationWithBlock:^{
sleep(1);
NSLog(@"bkOp3.....handle.....on thread num%@",[NSThread currentThread]);
}];
[bkOp3 setCompletionBlock:^{
NSLog(@"bkOp3........OK !!");
}];
NSBlockOperation * bkOp4 = [NSBlockOperation blockOperationWithBlock:^{
sleep(10);
NSLog(@"bkOp4.....handle.....on thread num%@",[NSThread currentThread]);
}];
[bkOp4 setCompletionBlock:^{
NSLog(@"bkOp4........OK !!");
}];
NSBlockOperation * bkOp5 = [NSBlockOperation blockOperationWithBlock:^{
sleep(5);
NSLog(@"bkOp5.....handle.....on thread num%@",[NSThread currentThread]);
}];
[bkOp5 setQueuePriority:NSOperationQueuePriorityHigh];
[bkOp5 setCompletionBlock:^{
NSLog(@"bkOp5........OK !!");
}];
NSInvocationOperation *invoOp6 = [[NSInvocationOperation alloc] initWithTarget:(id)self selector:@selector(handleInvoOp) object:nil];
[invoOp6 setCompletionBlock:^{
NSLog(@"invoOp6........OK !!");
}];
[invoOp6 setQueuePriority:NSOperationQueuePriorityHigh];
[qu setMaxConcurrentOperationCount:2];
[qu addOperation:bkOp3];
[qu addOperation:bkOp2];
[qu addOperation:bkOp1];
[qu addOperation:bkOp4];
[qu addOperation:bkOp5];
[qu addOperation:invoOp6];
}
先看下执行结果:
2014-03-22 17:18:36.634 Operation[17439:1803] bkOp3.....handle.....on thread num<NSThread: 0x1754c450>{name = (null), num = 2}
2014-03-22 17:18:36.638 Operation[17439:1803] bkOp3........OK !!
2014-03-22 17:18:37.635 Operation[17439:3607] bkOp2.....handle.....on thread num<NSThread: 0x1765a320>{name = (null), num = 3}
2014-03-22 17:18:37.637 Operation[17439:3607] bkOp2........OK !!
2014-03-22 17:18:41.640 Operation[17439:3d07] bkOp5.....handle.....on thread num<NSThread: 0x17562d10>{name = (null), num = 4}
2014-03-22 17:18:41.642 Operation[17439:3d07] bkOp5........OK !!
2014-03-22 17:18:42.639 Operation[17439:1803] invoOp6.....handle..... on thread num :<NSThread: 0x1754c450>{name = (null), num = 2}
2014-03-22 17:18:42.643 Operation[17439:1803] invoOp6........OK !!
2014-03-22 17:18:51.644 Operation[17439:3607] bkOp1.....handle.....on thread num<NSThread: 0x1765a320>{name = (null), num = 3}
2014-03-22 17:18:51.646 Operation[17439:3607] bkOp1........OK !!
2014-03-22 17:18:52.645 Operation[17439:3d07] bkOp4.....handle.....on thread num<NSThread: 0x17562d10>{name = (null), num = 4}
2014-03-22 17:18:52.647 Operation[17439:3d07] bkOp4........OK !!
从结果可以看出, NSOperationQueue在2,3,4这3个线程里去处理NSOperation,而不包括主线程1。此外,在设置了bkop5以及invOp6的优先级为高时,他们会优先执行,当然这个优先时相对,是相对正在排队的,不包括已经正在执行的。
总结:NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue都比较简单,NSOperation、NSBlockOperation、NSInvocationOperation单个都是表示一种操作,而NSOperationQueue是一个可以包含多个NSOperation的队列,可以自己在多个线程处理,只要加入队列之后,我们就不用去操作,直到Callback或者完成。
参考资料:
http://blog.csdn.net/totogo2010/article/details/8013316
http://www.cnblogs.com/mjios/archive/2013/04/19/3029765.html
————————————————
版权声明:本文为CSDN博主「hccheng」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/crycheng/article/details/21799611