iOS 開發多線程 —— NSOperation
本文是根據文頂頂老師的博客學習而來,轉載地址:http://www.cnblogs.com/wendingding/p/3809042.html
一、NSOperation簡介
1.簡單說明
NSOperation的作?:配合使用NSOperation和NSOperationQueue也能實現多線程編程
NSOperation和NSOperationQueue實現多線程的具體步驟:
(1)先將需要執行的操作封裝到一個NSOperation對象中
(2)然後將NSOperation對象添加到NSOperationQueue中
(3)系統會?動將NSOperationQueue中的NSOperation取出來
(4)將取出的NSOperation封裝的操作放到?條新線程中執?
2.NSOperation的子類
NSOperation是個抽象類,並不具備封裝操作的能力,必須使?它的子類
使用NSOperation?類的方式有3種:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自定義子類繼承NSOperation,實現內部相應的?法
NSInvocationOperation:
示例代碼:
- (void)viewDidLoad { [super viewDidLoad]; NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil]; [operation start]; NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil]; [operation2 start]; } -(void)thread1{ NSLog(@"currentThread = %@",[NSThread currentThread]); } -(void)thread2{ NSLog(@"currentThread2 = %@",[NSThread currentThread]); }
currentThread = <NSThread: 0x17406d780>{number = 1, name = main} currentThread2 = <NSThread: 0x17406d780>{number = 1, name = main}
可以看見,並沒有開啟新的線程.
註意:操作對象默認在主線程中執行,只有添加到隊列中才會開啟新的線程。即默認情況下,如果操作沒有放到隊列中queue中,都是同步執行。只有將NSOperation放到一個NSOperationQueue中,才會異步執行操作
NSBlockOperation:
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"operation = %@",[NSThread currentThread]); }]; [operation3 start]; 看打印結果: operation = <NSThread: 0x1700728c0>{number = 1, name = main}
註意:只要NSBlockOperation封裝的操作數 > 1,就會異步執行操作
例如:
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"operation = %@",[NSThread currentThread]); }]; [operation3 addExecutionBlock:^{ NSLog(@"operation2 = %@",[NSThread currentThread]); }]; [operation3 start]; 然後看打印結果: operation = <NSThread: 0x1700706c0>{number = 1, name = main} operation2 = <NSThread: 0x170075a40>{number = 4, name = (null)}
NSOperationQueue:
NSOperationQueue的作?:NSOperation可以調?start?法來執?任務,但默認是同步執行的
如果將NSOperation添加到NSOperationQueue(操作隊列)中,系統會自動異步執行NSOperation中的操作
添加操作到NSOperationQueue中,自動執行操作,自動開啟線程
示例:
- (void)viewDidLoad { [super viewDidLoad]; NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread1) object:nil]; NSInvocationOperation *operation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(thread2) object:nil]; NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"currentThread3 = %@",[NSThread currentThread]); }]; [operation3 addExecutionBlock:^{ NSLog(@"currentThread3_1 = %@",[NSThread currentThread]); }]; NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //把操作添加到隊列中 //第一種方式 [queue addOperation:operation1]; [queue addOperation:operation2]; [queue addOperation:operation3]; //第二種方式 [queue addOperationWithBlock:^{ NSLog(@"currentThread4 = %@",[NSThread currentThread]); }]; } -(void)thread1{ NSLog(@"currentThread1 = %@",[NSThread currentThread]); } -(void)thread2{ NSLog(@"currentThread2 = %@",[NSThread currentThread]); }
查看結果:
currentThread1 = <NSThread: 0x17006f440>{number = 4, name = (null)} currentThread2 = <NSThread: 0x17007d9c0>{number = 5, name = (null)} currentThread3_1 = <NSThread: 0x17007d9c0>{number = 5, name = (null)} currentThread3 = <NSThread: 0x17006f440>{number = 4, name = (null)} currentThread4 = <NSThread: 0x17007d9c0>{number = 5, name = (null)}
iOS 開發多線程 —— NSOperation