自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

NSOperationQueue和NSOperation的使用方法

移動開發(fā) iOS
多線程編程是防止主線程堵塞,增加運行效率等等的最佳方法。而原始的多線程方法存在很多的毛病,包括線程鎖死等。在Cocoa中,Apple提供了NSOperation這個類,提供了一個優(yōu)秀的多線程編程方法。

首先是建立NSOperationQueue和NSOperations。NSOperationQueue會建立一個線程管理器,每個加入到線程operation會有序的執(zhí)行。

  1. NSOperationQueue *queue = [NSOperationQueue new];    
  2. NSInvocationOperation *operation = [[NSInvocationOperation alloc];    
  3. initWithTarget:self    
  4. selector:@selector(doWork:)    
  5. object:someObject];    
  6. [queue addObject:operation];    
  7. [operation release];    

使用NSOperationQueue的過程:   

1.  建立一個NSOperationQueue的對象

2.  建立一個NSOperation的對象

3.  將operation加入到NSOperationQueue中

4.  release掉operation

NSInvocationOperation,NSInvocationOperation是NSOperation的子類,允許運行在operation中的targer和selector

多線程編程是防止主線程堵塞,增加運行效率等等的最佳方法。而原始的多線程方法存在很多的毛病,包括線程鎖死等。在Cocoa中,Apple提供了NSOperation這個類,提供了一個優(yōu)秀的多線程編程方法。

本次介紹NSOperation的子集,簡易方法的NSInvocationOperation:

  1. @implementation MyCustomClass    
  2. - (void)launchTaskWithData:(id)data    
  3. {    
  4.     //創(chuàng)建一個NSInvocationOperation對象,并初始化到方法    
  5.     //在這里,selector參數(shù)后的值是你想在另外一個線程中運行的方法(函數(shù),Method)    
  6.     //在這里,object后的值是想傳遞給前面方法的數(shù)據(jù)    
  7.     NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self    
  8.                     selector:@selector(myTaskMethod:) object:data];    
  9.     // 下面將我們建立的操作“Operation”加入到本地程序的共享隊列中(加入后方法就會立刻被執(zhí)行)    
  10.     // 更多的時候是由我們自己建立“操作”隊列    
  11.     [[MyAppDelegate sharedOperationQueue] addOperation:theOp];    
  12. }    
  13. // 這個是真正運行在另外一個線程的“方法”    
  14. - (void)myTaskMethod:(id)data    
  15. {    
  16.     // Perform the task.    
  17. }    
  18. @end   

一個NSOperationQueue 操作隊列,就相當于一個線程管理器,而非一個線程。因為你可以設置這個線程管理器內(nèi)可以并行運行的的線程數(shù)量等等。下面是建立并初始化一個操作隊列:

  1. @interface MyViewController : UIViewController {    
  2.     NSOperationQueue *operationQueue;    
  3.     //在頭文件中聲明該隊列    
  4. }    
  5. @end    
  6. @implementation MyViewController    
  7. - (id)init    
  8. {    
  9.     self = [super init];    
  10.     if (self) {    
  11.         operationQueue = [[NSOperationQueue alloc] init]; //初始化操作隊列    
  12.         [operationQueue setMaxConcurrentOperationCount:1];    
  13.         //在這里限定了該隊列只同時運行一個線程    
  14.         //這個隊列已經(jīng)可以使用了    
  15.     }    
  16.     return self;    
  17. }    
  18. - (void)dealloc    
  19. {    
  20.     [operationQueue release];    
  21.     //正如Alan經(jīng)常說的,我們是程序的好公民,需要釋放內(nèi)存!    
  22.     [super dealloc];    
  23. }    
  24. @end  

簡單介紹之后,其實可以發(fā)現(xiàn)這種方法是非常簡單的。很多的時候我們使用多線程僅僅是為了防止主線程堵塞,而NSInvocationOperation就是最簡單的多線程編程,在iPhone編程中是經(jīng)常被用到的。

責任編輯:閆佳明 來源: iteye
相關推薦

2011-04-08 10:43:44

2011-09-07 10:36:58

ubuntuUbuntuOne

2018-06-20 10:34:56

堆棧iOSswift

2012-01-13 09:55:54

jQuery

2011-02-24 13:09:10

FireFTP

2010-02-22 18:46:31

2009-08-21 18:00:38

ASP.NET mac

2014-05-06 10:20:02

2011-05-20 17:05:59

ADO.NET

2010-05-04 14:02:53

Oracle同義詞

2009-12-02 16:04:44

PHP fsockop

2012-03-06 10:17:45

iOS SQLite3iOSSQLite3

2023-02-08 08:40:21

2009-12-24 16:36:06

WPF InkCanv

2010-01-06 10:18:02

JSON類

2010-10-08 16:01:17

mysql UPDAT

2010-05-18 14:06:22

SubVersion和

2011-07-20 14:45:43

C++結(jié)構(gòu)體

2009-12-31 16:04:39

ADO.NET技術(shù)

2010-01-06 15:03:34

JSON格式封裝
點贊
收藏

51CTO技術(shù)棧公眾號