1、NSOperation簡介編程
1.簡單說明多線程
NSOperation的做⽤:配合使用NSOperation和NSOperationQueue也能實現多線程編程併發
NSOperation和NSOperationQueue實現多線程的具體步驟:異步
(1)先將須要執行的操做封裝到一個NSOperation對象中spa
(2)而後將NSOperation對象添加到NSOperationQueue中線程
(3)系統會⾃動將NSOperationQueue中的NSOperation取出來code
(4)將取出的NSOperation封裝的操做放到⼀條新線程中執⾏對象
2.NSOperation的子類blog
NSOperation是個抽象類,並不具有封裝操做的能力,必須使⽤它的子類繼承
使用NSOperation⼦類的方式有3種:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自定義子類繼承NSOperation,實現內部相應的⽅法
2、 具體說明
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