Cocoa: NSOperation和NSOperationQueue
在任何语言中多线程处理都是麻烦的。更糟糕的是如果出错了往往会以很坏的方式出错。鉴于此,程序员要么完全避免使用多线程(把它当做邪恶之源),要么发很长的时间来确保每个方面都很完美。
- #import <Cocoa/Cocoa.h>
- @interface PageLoadOperation : NSOperation {
- NSURL *targetURL;
- }
- @property(retain) NSURL *targetURL;
- - (id)initWithURL:(NSURL*)url;
- @end
PageLoadOperation.m
- #import "PageLoadOperation.h"
- #import "AppDelegate.h"
- @implementation PageLoadOperation
- @synthesize targetURL;
- - (id)initWithURL:(NSURL*)url;
- {
- if (![super init]) return nil;
- [self setTargetURL:url];
- return self;
- }
- - (void)dealloc {
- [targetURL release], targetURL = nil;
- [super dealloc];
- }
- - (void)main {
- NSString *webpageString = [[[NSString alloc] initWithContentsOfURL:[self targetURL]] autorelease];
- NSError *error = nil;
- NSXMLDocument *document = [[NSXMLDocument alloc] initWithXMLString:webpageString
- options:NSXMLDocumentTidyHTML
- error:&error];
- if (!document) {
- NSLog(@"%s Error loading document (%@): %@", _cmd, [[self targetURL] absoluteString], error);
- return;
- }
- [[AppDelegate shared] performSelectorOnMainThread:@selector(pageLoaded:)
- withObject:document
- waitUntilDone:YES];
- [document release];
- }
- @end
- #import <Cocoa/Cocoa.h>
- @interface AppDelegate : NSObject {
- NSOperationQueue *queue;
- }
- + (id)shared;
- - (void)pageLoaded:(NSXMLDocument*)document;
- @end
- #import "AppDelegate.h"
- #import "PageLoadOperation.h"
- @implementation AppDelegate
- static AppDelegate *shared;
- static NSArray *urlArray;
- - (id)init
- {
- if (shared) {
- [self autorelease];
- return shared;
- }
- if (![super init]) return nil;
- NSMutableArray *array = [[NSMutableArray alloc] init];
- [array addObject:@"http://www.google.com"];
- [array addObject:@"http://www.apple.com"];
- [array addObject:@"http://www.yahoo.com"];
- [array addObject:@"http://www.zarrastudios.com"];
- [array addObject:@"http://www.macosxhints.com"];
- urlArray = array;
- queue = [[NSOperationQueue alloc] init];
- shared = self;
- return self;
- }
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
- {
- for (NSString *urlString in urlArray) {
- NSURL *url = [NSURL URLWithString:urlString];
- PageLoadOperation *plo = [[PageLoadOperation alloc] initWithURL:url];
- [queue addOperation:plo];
- [plo release];
- }
- }
- - (void)dealloc
- {
- [queue release], queue = nil;
- [super dealloc];
- }
- + (id)shared;
- {
- if (!shared) {
- [[AppDelegate alloc] init];
- }
- return shared;
- }
- - (void)pageLoaded:(NSXMLDocument*)document;
- {
- NSLog(@"%s Do something with the XMLDocument: %@", _cmd, document);
- }
- @end
- - (id)init
- {
- if (shared) {
- [self autorelease];
- return shared;
- }
- if (![super init]) return nil;
- NSMutableArray *array = [[NSMutableArray alloc] init];
- [array addObject:@"http://www.google.com"];
- [array addObject:@"http://www.apple.com"];
- [array addObject:@"http://www.yahoo.com"];
- [array addObject:@"http://www.zarrastudios.com"];
- [array addObject:@"http://www.macosxhints.com"];
- urlArray = array;
- queue = [[NSOperationQueue alloc] init];
- [queue setMaxConcurrentOperationCount:2];
- shared = self;
- return self;
- }