#iOS多線程學習&總結
在查閱資料文檔,系統(tǒng)得重新審視了下iOS多線程。下面是自己結合資料做的一些整理,比較偏基礎。
后續(xù)還會有一篇關于線程間通信的整理
##基本概念
- 進程:一個具有一定獨立功能的程序關于某個數(shù)據(jù)集合的一次運動活動??梢岳斫獬梢粋€運行中的應用程序。
- 線程:程序執(zhí)行流的最小單元,線程是進程中的一個實體。
- 同步:只能在當前線程按先后順序依次執(zhí)行,不開啟新線程。
- 異步:可以在當前線程開啟多個新線程執(zhí)行,可以不按順序執(zhí)行。
- 隊列: 裝載線程任務的隊形結構。
- 并發(fā):線程執(zhí)行可以同時一起執(zhí)行。
- 串行:線程執(zhí)行只能依次逐一先后有序的執(zhí)行。

##iOS多線程對比
###NSThread
每個NSThread對象對應一個線程,真正最原始的線程。
- 優(yōu)點:NSThread 輕量級,相對使用簡單。
- 缺點:需要手動管理線程活動,控制生命周期、線程加鎖、睡眠、喚醒等。
###NSOperation
- 優(yōu)點:不需要關心線程管理,重心可以放在執(zhí)行邏輯上。
- 缺點:面向對象的線程技術,只能使用NSOperation的兩個子類。
1. NSInvocationOperation:實例方法創(chuàng)建線程。
2. NSBlockOperation:類方法創(chuàng)建線程,在Block里是事件內容。
###GCD
多核編程、對底層C語言的封裝,蘋果推薦使用。
- 優(yōu)點:最高效,避開并發(fā)陷阱。
- 缺點:基于C實現(xiàn)。
##多線程如何使用
###NSThread
####三種實現(xiàn)開啟線程方式:
- 動態(tài)實例化
```
NSThread *thread = [[NSThread alloc] initWithTarget:self
selector:@selector(loadImg:)
object:imgUrl];
thread.threadPriority = 1;
[thread start];
```
- 靜態(tài)實例化
```
[NSThread detachNewThreadSelector:@selector(loadImg:)
toTarget:self
withObject:imgUrl];
```
- 隱式實例化
```
[self performSelectorInBackground:@selector(loadImg:)
withObject:imgUrl];
```
###NSOperation
主要的實現(xiàn)方式:結合NSOperation和NSOperationQueue實現(xiàn)多線程編程。
- 實例化NSOperation的子類,綁定執(zhí)行的操作。
- 創(chuàng)建NSOperationQueue隊列,將NSOperation實例添加進來。
- 系統(tǒng)會自動將NSOperationQueue隊列中檢測取出和執(zhí)行NSOperation的操作。
####使用NSOperation的子類實現(xiàn)創(chuàng)作線程
- NSInvocationOperation創(chuàng)建線程
```
NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(loadImg:)
object:imgUrl];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperation:invocationOperation];
```
- NSBlockOperation創(chuàng)建線程
```
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
[self loadImageSource:imgUrl];
}];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperation:blockOperation];
```
###GCD
高性能的多線程解決方案。
- 主線程隊列(刷新UI)
`dispatch_get_main_queue()`
- 并行隊列
`dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)`
```
DISPATCH_QUEUE_PRIORITY_DEFAULT:默認優(yōu)先級
DISPATCH_QUEUE_PRIORITY_HIGH:高優(yōu)先級
DISPATCH_QUEUE_PRIORITY_LOW:低優(yōu)先級
```
- 串行隊列
`dispatch_queue_create("queue_name", NULL)`
####GCD多線程實現(xiàn)(六種)
- 主線程執(zhí)行
```
dispatch_async(dispatch_get_main_queue(), ^{
[self loadImg:imgUrl];
});
```
- 后臺執(zhí)行線程
```
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self loadImg:imgUrl];
});
```
- 延遲執(zhí)行
```
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,TIME * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self loadImg:imgUrl];
});
```
- 一次性執(zhí)行(可用來寫單例)
```
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self loadImg:imgUrl];
});
```
- 并發(fā)執(zhí)行循環(huán)迭代
```
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
size_t count = number;
dispatch_apply(count, queue, ^(size_t i) {
[self loadImg:imgUrl];
});
```
- 自定義隊列
```
dispatch_queue_t customQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(customQueue, ^{
[self loadImg:imgUrl];
});
```