標籤:自己 gcd 建立 tar current ota art url 問題
多線程
1.NSThread
1> 開線程的幾種方式
* 先建立,後啟動
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
* 直接啟動
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
[self performSelectorInBackground:@selector(run) withObject:nil];
2> 其他用法
NSThread *current = [NSThread currentThread];
+ (NSThread *)mainThread; // 獲得主線程
3> 線程間通訊
performSelectorOnMainThread.....
2.GCD(重點)
1> 隊列的類型
* 並發隊列
獲得全域的並發隊列: dispatch_get_global_queue
* 串列隊列
a.自己建立
dispatch_queue_create
b.主隊列
dispatch_get_main_queue
2> 執行任務的方法類型
* 同步(sync)執行
* 非同步(async)執行
3> 瞭解隊列和方法的配合使用
4> 線程間通訊
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 執行耗時的非同步作業...
dispatch_async(dispatch_get_main_queue(), ^{
// 回到主線程,執行UI重新整理操作
});
});
5> 其他用法
dispatch_once
dispatch_after
dispatch_group_async\dispatch_group_notify
3.NSOperation
1> 基本使用
NSInvocationOperation
NSBlockOperation
2> NSOperationQueue(重點)
* 最大並發數設定
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
* 設定依賴(面試題)
[operationB addDependency:operationA]; // 操作B依賴於操作A
3> 自訂Operation(瞭解基本流程)
4> 如何解決一張圖片(一個url)重複下載的問題(面試題)
IOS 線程的總結