iOS開發多線程篇—NSOperation簡單介紹
一、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,實現內部相應的⽅法
二、 具體說明
1.NSInvocationOperation子類
創建對象和執行操作:
1 //創建操作對象,封裝要執行的任務 2 //NSInvocationOperation 封裝操作 3 NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil]; 4 5 //執行操作 6 [operation start];
說明:一旦執⾏操作,就會調用target的test方法
代碼示例:
1 // 2 // YYViewController.m 3 // 01-NSOperation基本1 4 // 5 // Created by 孔醫己 on 14-6-25. 6 // Copyright (c) 2014年 itcast. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 //NSOperation:抽象類,不具備封裝功能 22 23 //創建操作對象,封裝要執行的任務 24 //NSInvocationOperation 封裝操作 25 NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil]; 26 27 //執行操作 28 [operation start]; 29 30 } 31 32 -(void)test 33 { 34 35 NSLog(@"--test--%@--",[NSThread currentThread]); 36 } 37 @end
打印查看:
注意:操作對象默認在主線程中執行,只有添加到隊列中才會開啟新的線程。即默認情況下,如果操作沒有放到隊列中queue中,都是同步執行。只有將NSOperation放到一個NSOperationQueue中,才會異步執行操作
2.NSBlockOperation子類
創建對象和添加操作:
1 //創建NSBlockOperation操作對象 2 NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{ 3 //...... 4 }]; 5 6 //添加操作 7 [operation addExecutionBlock:^{ 8 //.... 9 }]; 10
代碼示例:
代碼1:
1 // 2 // YYViewController.m 3 // 02-NSTherad基本2 4 // 5 // Created by 孔醫己 on 14-6-25. 6 // Copyright (c) 2014年 itcast. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 //創建NSBlockOperation操作對象 22 NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{ 23 NSLog(@"NSBlockOperation------%@",[NSThread currentThread]); 24 }]; 25 26 27 //開啟執行操作 28 [operation start]; 29 } 30 @end
打印查看:
代碼2:
1 // 2 // YYViewController.m 3 // 02-NSTherad基本2 4 // 5 // Created by 孔醫己 on 14-6-25. 6 // Copyright (c) 2014年 itcast. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 //創建NSBlockOperation操作對象 22 NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{ 23 NSLog(@"NSBlockOperation------%@",[NSThread currentThread]); 24 }]; 25 26 //添加操作 27 [operation addExecutionBlock:^{ 28 NSLog(@"NSBlockOperation1------%@",[NSThread currentThread]); 29 }]; 30 31 [operation addExecutionBlock:^{ 32 NSLog(@"NSBlockOperation2------%@",[NSThread currentThread]); 33 }]; 34 35 //開啟執行操作 36 [operation start]; 37 } 38 @end
注意:只要NSBlockOperation封裝的操作數 > 1,就會異步執行操作
3.NSOperationQueue
NSOperationQueue的作⽤:NSOperation可以調⽤start⽅法來執⾏任務,但默認是同步執行的
如果將NSOperation添加到NSOperationQueue(操作隊列)中,系統會自動異步執行NSOperation中的操作
添加操作到NSOperationQueue中,自動執行操作,自動開啟線程
1 //創建NSOperationQueue 2 NSOperationQueue * queue=[[NSOperationQueue alloc]init]; 3 //把操作添加到隊列中 4 //第一種方式 5 [queue addOperation:operation1]; 6 [queue addOperation:operation2]; 7 [queue addOperation:operation3]; 8 //第二種方式 9 [queue addOperationWithBlock:^{ 10 NSLog(@"NSBlockOperation3--4----%@",[NSThread currentThread]); 11 }];
- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block;
代碼示例:
1 // 2 // YYViewController.m 3 // 03-NSOperation基本3 4 // 5 // Created by 孔醫己 on 14-6-25. 6 // Copyright (c) 2014年 itcast. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 //創建NSInvocationOperation對象,封裝操作 22 NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil]; 23 NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil]; 24 //創建對象,封裝操作 25 NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{ 26 NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]); 27 }]; 28 [operation3 addExecutionBlock:^{ 29 NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]); 30 }]; 31 32 //創建NSOperationQueue 33 NSOperationQueue * queue=[[NSOperationQueue alloc]init]; 34 //把操作添加到隊列中 35 [queue addOperation:operation1]; 36 [queue addOperation:operation2]; 37 [queue addOperation:operation3]; 38 } 39 40 -(void)test1 41 { 42 NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]); 43 } 44 45 -(void)test2 46 { 47 NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]); 48 } 49 50 @end
打印效果:
注意:系統自動將NSOperationqueue中的NSOperation對象取出,將其封裝的操作放到一條新的線程中執行。上面的代碼示例中,一共有四個任務,operation1和operation2分別有一個任務,operation3有兩個任務。一共四個任務,開啟了四條線程。通過任務執行的時間全部都是273可以看出,這些任務是並行執行的。
下面使用for循環打印,可以更明顯的看出任務是並發執行的。
代碼示例:
1 #import "YYViewController.h" 2 3 @interface YYViewController () 4 5 @end 6 7 @implementation YYViewController 8 9 - (void)viewDidLoad 10 { 11 [super viewDidLoad]; 12 13 //創建NSInvocationOperation對象,封裝操作 14 NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil]; 15 NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil]; 16 //創建對象,封裝操作 17 NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{ 18 for (int i=0; i<5; i++) { 19 NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]); 20 } 21 }]; 22 [operation3 addExecutionBlock:^{ 23 for (int i=0; i<5; i++) { 24 NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]); 25 } 26 }]; 27 28 //創建NSOperationQueue 29 NSOperationQueue * queue=[[NSOperationQueue alloc]init]; 30 //把操作添加到隊列中 31 [queue addOperation:operation1]; 32 [queue addOperation:operation2]; 33 [queue addOperation:operation3]; 34 } 35 36 -(void)test1 37 { 38 for (int i=0; i<5; i++) { 39 NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]); 40 } 41 } 42 43 -(void)test2 44 { 45 for (int i=0; i<5; i++) { 46 NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]); 47 } 48 } 49 50 @end