现在的位置: 首页 > 综合 > 正文

多线程(NSOperation和NSOperationQueue)

2013年09月01日 ⁄ 综合 ⁄ 共 2189字 ⁄ 字号 评论关闭

多线程(NSOperation和NSOperationQueue)

在网络应用程序中,经常要使用多任务处理来提高应用程序的性能,即在同一时间,有多个处理同时进行。例如,同时进行多个文件下载,同时进行多个HTTP 请求等。这一般都

是通过多线程完成的。另外,多线程编程也是为了防止主线程堵塞,增加运行效率的方法。比如,如果主线程从网上下载一个很大的图片,那么,给用户的感觉是整个应用程序死掉了。所以,可以把这个下载操作放在一个线程中,在主线程中调用这个线程让它在后台处理,主线程序可以显示一些其他信息,比如显示一些“正在装载”等文字信息。

在Cocoa中,NSOperation类提供了一个优秀的多线程编程方法。很多编程语言都支持多线程处理应用程序,但是多线程程序往往一旦出错就会很难处理。庆幸的是,苹果公司在这方面做了很多改进,例如在NSThread 上新增了很多方法,还新增了两个类NSOperation 和NSOperationQueue,从而让多线程处理变得更加容易。

在多线程中,可以同时进行多个操作。NSOperation 对象就是一个操作,比如,装载网页内容的一个操作。在Objective-C 上,一个具体的操作(比如网页装载)是一个继承NSOperation 的类。在这个类中,至少需要重写一个-(void)main 方法。线程(NSOperation)自动调用main 方法,main 方法就是线程要执行的具体操作。在下面的例子中,PageLoadOperation 继承了NSOperation,并实现了main 方法。一般而言,可以利用其初始化方法来传入所需要的参数和对象,比如PageLoadOperation的initWithURL:方法用来设置要装载的网址。

使用NSOperation 的最简单方法就是将其放入NSOperationQueue 中,NSOperationQueue是存放多个操作的队列。一旦一个NSOperation 对象被加入NSOperationQueue,该队列就会启动并开始处理它(即调用它的main方法),当操作完成后,队列就会释放它。

下面创建一个Cocoa Application例子来演示使用NSOperation和NSOperationQueue完成多线程处理。

应用代理类AppDelegate.h的代码如下:

  1. #import <Cocoa/Cocoa.h> 
  2. @interface AppDelegate : NSObject {  
  3. NSOperationQueue *queue; //线程队列  
  4. }  
  5. + (id)shared;  
  6. - (void)pageLoaded:(NSXMLDocument*)document;  
  7. @end  
  8.  
  9. AppDelegate.m的代码如下:  
  10.  
  11. #import "AppDelegate.h"  
  12. #import "PageLoadOperation.h"  
  13. @implementation AppDelegate  
  14. static AppDelegate *shared;  
  15. static NSArray *urlArray;  
  16. - (id)init  
  17. {  
  18. if (shared) {  
  19. [self autorelease];  
  20. return shared;  
  21. }  
  22. if (![super init]) return nil;  
  23. //设置要访问的网址  
  24. NSMutableArray *array = [[NSMutableArray alloc] init];  
  25. [array addObject:@"http://www.xinlaoshi.com"];  
  26. [array addObject:@"http://www.yunwenjian.com"];  
  27. [array addObject:@"http://www.108fang.com"];  
  28. [array addObject:@"http://www.baidu.com"];  
  29. urlArray = array;  
  30. //[queue setMaxConcurrentOperationCount:2];  
  31. queue = [[NSOperationQueue alloc] init];  
  32. shared = self;  
  33. return self;  
  34. }  
  35. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification  
  36. { //把各个操作添加到队列中  
  37. for (NSString *urlString in urlArray) {  
  38. NSURL *url = [NSURL URLWithString:urlString];  
  39. PageLoadOperation *plo = [[PageLoadOperation alloc]  
  40. initWithURL:url];  
  41. [queue addOperation:plo];  
  42. [plo release];  
  43. }  
  44. }  
  45. - (void)dealloc  
  46. {  
  47. [queue release], queue = nil;  
  48. [super dealloc];  
  49. }  
  50. + (id)shared;  
  51. {  
  52. if (!shared) {  
  53. [[AppDelegate alloc] init];  
  54. }  
  55. return shared;  

抱歉!评论已关闭.