每個NSThread對象對應一個線程,量級較輕(真正的多線程)NSOperation/NSOperationQueue 物件導向的線程技術(派發) 是基於C語言的架構,可以充分利用多核,是蘋果推薦使用的多線程技術
,在項目中很多架構技術分別使用了不同多線程技術。
三種多線程技術的對比
。線程同步對資料的加鎖會有一定的系統開銷 。iOS4.0+才能使用,是替代NSThread, NSOperation的高效和強大的技術
[NSThread detachNewThreadSelector:@selector(bigDemo) toTarget:self withObject:nil];
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(bigDemo) [thread start];
[self performSelectorInBackground:@selector(bigDemo) withObject:nil];
}
_queue = [[NSOperationQueue alloc] init];
NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(opAction) [_queue addOperation:op1];
mark 模仿下載網狀圖像 - (IBAction)operationDemo3:( NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^ NSLog( NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^ NSLog( NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^ NSLog( }
[_queue addOperation:op2];
^
[_queue setMaxConcurrentOperationCount:];
[op1 addDependency:op3];
全域
隊列
非同步
同步
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
dispatch_queue_t queue = dispatch_queue_create(, DISPATCH_QUEUE_SERIAL);
dispatch_async(dispatch_get_main_queue(), ^ NSLog( });
清澈Saup