IOS学习之多线程(9)--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,實現內部相應的法
二、 具體說明
1.NSInvocationOperation子類
創建對象和執行操作:
//創建操作對象,封裝要執行的任務//NSInvocationOperation???封裝操作NSInvocationOperation?*operation=[[NSInvocationOperation?alloc]initWithTarget:self?selector:@selector(test)?object:nil];//執行操作[operation?start];說明:一旦執操作,就會調用target的test方法
代碼示例:
// //??YYViewController.m //??01-NSOperation基本1 // //??Created?by?孔醫己?on?14-6-25. //??Copyright?(c)?2014年?itcast.?All?rights?reserved. // #import?"YYViewController.h" @interface?YYViewController?() @end @implementation?YYViewController -?(void)viewDidLoad {[super?viewDidLoad];//NSOperation:抽象類,不具備封裝功能//創建操作對象,封裝要執行的任務//NSInvocationOperation???封裝操作NSInvocationOperation?*operation=[[NSInvocationOperation?alloc]initWithTarget:self?selector:@selector(test)?object:nil];//執行操作[operation?start]; } -(void)test {NSLog(@"--test--%@--",[NSThread?currentThread]); } @end打印查看:
注意:操作對象默認在主線程中執行,只有添加到隊列中才會開啟新的線程。即默認情況下,如果操作沒有放到隊列中queue中,都是同步執行。只有將NSOperation放到一個NSOperationQueue中,才會異步執行操作?
?
2.NSBlockOperation子類
創建對象和添加操作:
//創建NSBlockOperation操作對象NSBlockOperation?*operation=[NSBlockOperation?blockOperationWithBlock:^{//......}];//添加操作[operation?addExecutionBlock:^{//....}];代碼示例:
代碼1:
// //??YYViewController.m //??02-NSTherad基本2 // //??Created?by?孔醫己?on?14-6-25. //??Copyright?(c)?2014年?itcast.?All?rights?reserved. // #import?"YYViewController.h" @interface?YYViewController?() @end @implementation?YYViewController -?(void)viewDidLoad {[super?viewDidLoad];//創建NSBlockOperation操作對象NSBlockOperation?*operation=[NSBlockOperation?blockOperationWithBlock:^{NSLog(@"NSBlockOperation------%@",[NSThread?currentThread]);}];//開啟執行操作[operation?start]; } @end打印查看:
代碼2:
// //??YYViewController.m //??02-NSTherad基本2 // //??Created?by?孔醫己?on?14-6-25. //??Copyright?(c)?2014年?itcast.?All?rights?reserved. // #import?"YYViewController.h" @interface?YYViewController?() @end @implementation?YYViewController -?(void)viewDidLoad {[super?viewDidLoad];//創建NSBlockOperation操作對象NSBlockOperation?*operation=[NSBlockOperation?blockOperationWithBlock:^{NSLog(@"NSBlockOperation------%@",[NSThread?currentThread]);}];//添加操作[operation?addExecutionBlock:^{NSLog(@"NSBlockOperation1------%@",[NSThread?currentThread]);}];[operation?addExecutionBlock:^{NSLog(@"NSBlockOperation2------%@",[NSThread?currentThread]);}];//開啟執行操作[operation?start]; } @end注意:只要NSBlockOperation封裝的操作數 > 1,就會異步執行操作?
?
3.NSOperationQueue
NSOperationQueue的作:NSOperation可以調start法來執任務,但默認是同步執行的
如果將NSOperation添加到NSOperationQueue(操作隊列)中,系統會自動異步執行NSOperation中的操作
添加操作到NSOperationQueue中,自動執行操作,自動開啟線程
//創建NSOperationQueueNSOperationQueue?*?queue=[[NSOperationQueue?alloc]init];//把操作添加到隊列中//第一種方式[queue?addOperation:operation1];[queue?addOperation:operation2];[queue?addOperation:operation3];//第二種方式[queue?addOperationWithBlock:^{NSLog(@"NSBlockOperation3--4----%@",[NSThread?currentThread]);}];- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block;?
代碼示例:
// //??YYViewController.m //??03-NSOperation基本3 // //??Created?by?孔醫己?on?14-6-25. //??Copyright?(c)?2014年?itcast.?All?rights?reserved. // #import?"YYViewController.h" @interface?YYViewController?() @end @implementation?YYViewController -?(void)viewDidLoad {[super?viewDidLoad];//創建NSInvocationOperation對象,封裝操作NSInvocationOperation?*operation1=[[NSInvocationOperation?alloc]initWithTarget:self?selector:@selector(test1)?object:nil];NSInvocationOperation?*operation2=[[NSInvocationOperation?alloc]initWithTarget:self?selector:@selector(test2)?object:nil];//創建對象,封裝操作NSBlockOperation?*operation3=[NSBlockOperation?blockOperationWithBlock:^{NSLog(@"NSBlockOperation3--1----%@",[NSThread?currentThread]);}];[operation3?addExecutionBlock:^{NSLog(@"NSBlockOperation3--2----%@",[NSThread?currentThread]);}];//創建NSOperationQueueNSOperationQueue?*?queue=[[NSOperationQueue?alloc]init];//把操作添加到隊列中[queue?addOperation:operation1];[queue?addOperation:operation2];[queue?addOperation:operation3]; } -(void)test1 {NSLog(@"NSInvocationOperation--test1--%@",[NSThread?currentThread]); } -(void)test2 {NSLog(@"NSInvocationOperation--test2--%@",[NSThread?currentThread]); } @end打印效果:
注意:系統自動將NSOperationqueue中的NSOperation對象取出,將其封裝的操作放到一條新的線程中執行。上面的代碼示例中,一共有四個任務,operation1和operation2分別有一個任務,operation3有兩個任務。一共四個任務,開啟了四條線程。通過任務執行的時間全部都是273可以看出,這些任務是并行執行的。
提示:隊列的取出是有順序的,與打印結果并不矛盾。這就好比,選手A,BC雖然起跑的順序是先A,后B,然后C,但是到達終點的順序卻不一定是A,B在前,C在后。
下面使用for循環打印,可以更明顯的看出任務是并發執行的。
代碼示例:
#import?"YYViewController.h" @interface?YYViewController?() @end @implementation?YYViewController -?(void)viewDidLoad {[super?viewDidLoad];//創建NSInvocationOperation對象,封裝操作NSInvocationOperation?*operation1=[[NSInvocationOperation?alloc]initWithTarget:self?selector:@selector(test1)?object:nil];NSInvocationOperation?*operation2=[[NSInvocationOperation?alloc]initWithTarget:self?selector:@selector(test2)?object:nil];//創建對象,封裝操作NSBlockOperation?*operation3=[NSBlockOperation?blockOperationWithBlock:^{for?(int?i=0;?i<5;?i++)?{NSLog(@"NSBlockOperation3--1----%@",[NSThread?currentThread]);}}];[operation3?addExecutionBlock:^{for?(int?i=0;?i<5;?i++)?{NSLog(@"NSBlockOperation3--2----%@",[NSThread?currentThread]);}}];//創建NSOperationQueueNSOperationQueue?*?queue=[[NSOperationQueue?alloc]init];//把操作添加到隊列中[queue?addOperation:operation1];[queue?addOperation:operation2];[queue?addOperation:operation3]; } -(void)test1 {for?(int?i=0;?i<5;?i++)?{NSLog(@"NSInvocationOperation--test1--%@",[NSThread?currentThread]);} } -(void)test2 {for?(int?i=0;?i<5;?i++)?{NSLog(@"NSInvocationOperation--test2--%@",[NSThread?currentThread]);} } @end轉載于:https://blog.51cto.com/5934497/1718215
總結
以上是生活随笔為你收集整理的IOS学习之多线程(9)--NSOperation简单介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 打印100~200 之间的素数
- 下一篇: 你真的了解实时计算吗?