IOS 線程的總結

來源:互聯網
上載者:User

標籤:自己   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 線程的總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.