1、好處:
1谱秽、使用線程可以把程序中占據(jù)時間長的任務放到后臺去處理,如圖片郊供、視頻的下載
2听绳、發(fā)揮多核處理器的優(yōu)勢颂碘,并發(fā)執(zhí)行讓系統(tǒng)運行的更快、更流暢塔拳,用戶體驗更好
缺點:
1峡竣、大量的線程降低代碼的可讀性,
2适掰、更多的線程需要更多的內(nèi)存空間
3、當多個線程對同一個資源出現(xiàn)爭奪的時候要注意線程安全的問題类浪。
iOS有三種多線程編程的技術(shù):
1、NSThread(兩種創(chuàng)建方式)
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:nil];
NSThread *myThread = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
[myThread start];
2诉瓦、NSOperationQueue
NSOperationQueue*oprationQueue= [[NSOperationQueuealloc] init];
oprationQueueaddOperationWithBlock:^{
//這個block語句塊在子線程中執(zhí)行
}
http://alloc.sinaapp.com/wp/?p=237
3力细、Grand Central Dispatch (GCD)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//耗時的操作
dispatch_async(dispatch_get_main_queue(), ^{
//更新界面
});
});
http://blog.csdn.net/totogo2010/article/details/8016129
PS:不顯示的創(chuàng)建線程的方法:
用NSObject的類方法performSelectorInBackground:withObject:創(chuàng)建一個線程:[Obj performSelectorInBackground:@selector(doSomething) withObject:nil];