iOS 多執行緒詳解

極客學偉發表於2018-05-15

iOS 多執行緒詳解

Slogan : 可能是最通俗易懂的 iOS多執行緒 詳細解析文章

1. 基礎概念

1.1 程式

程式是計算機中已執行程式的實體,是執行緒的容器維基百科-程式。每個程式之間是相互獨立的,每個程式均執行在器專用且收保護的記憶體空間內。 把工廠作為一個系統,程式類似於車間。

1.2 執行緒

執行緒是作業系統能夠進行運算排程的最小單位維基百科-執行緒。一個程式的所有任務都線上程中執行。一個執行緒中執行的任務是序列的,同一時間內1個執行緒只能執行一個任務。 把工廠作為一個系統,執行緒類似於車間裡幹活的工人。

1.3 程式和執行緒之間關係

  1. 執行緒是CPU呼叫的最小單位
  2. 程式手機CPU分配資源的最小單位
  3. 一個程式中至少有一個執行緒
  4. 同一個程式內的執行緒共享程式的資源

1.4 多執行緒

一個程式可以開啟多條執行緒,每條執行緒可以同時執行不同的任務,多執行緒技術可以提高程式的執行效率。同一時間內,CPU只能處理1條執行緒,只有1條執行緒在工作,多執行緒併發執行,其實是CPU快速的在多條執行緒之間排程,如果CPU排程執行緒的時間足夠快,就造成了多執行緒併發執行的假象。CPU在多條執行緒之間排程會消耗大量的CPU資源,同時每條執行緒被排程的頻次會降低,因此我們只開闢3-5條執行緒。

1.5 多執行緒優缺點

優點:1、能適當提高程式的執行效率;2、能適當提高資源利用率(CPU,記憶體利用率) 缺點: 1、建立執行緒的開銷,在iOS中,核心資料結構(大約1kb)、棧空間(子執行緒512kb,主執行緒1MB)建立執行緒大約需要90毫秒的建立時間,如果開啟大量執行緒會降低程式效能,執行緒越多,CPU在排程執行緒上的開銷就越大。

1.6 執行緒的狀態

執行緒的狀態

  1. 建立:例項化物件
  2. 就緒:向執行緒物件傳送start訊息,執行緒物件被加入 “可排程執行緒池”,等待CPU排程,detach 方法 和 performSelectorInBackground 方法會直接例項化一個執行緒物件並加入 “可排程執行緒池”
  3. 執行:CPU 負責排程 “可排程執行緒池”中執行緒的執行,執行緒執行完成之前,狀態可能會在 “就緒” 和 “執行” 之間來回切換,此過程CPU控制。
  4. 阻塞:當滿足某個預定條件時,可以使用休眠或鎖阻塞執行緒執行,影響的方法有:sleepForTimeInterval, sleepUntilDate, @synchronized(self) 執行緒鎖。執行緒物件進入阻塞狀態後,會被“可排程執行緒池” 中移除,CPU不再排程。
  5. 死亡:死亡後執行緒物件的 isFinished 屬性為YES;如果傳送cancel訊息,執行緒物件的 isCanceled 屬性為YES;死亡後 stackSize == 0, 記憶體空間被釋放。

1.7 執行緒鎖的幾種方案

執行緒鎖效率
加解鎖速度不表示鎖的效率,只表示加解鎖操作在執行時的複雜程度。

1.7.1 互斥鎖

@synchronized(鎖物件) { 
    // 需要鎖定的程式碼  
}
複製程式碼

使用互斥鎖,在同一個時間,只允許一條執行緒執行鎖中的程式碼。因為互斥鎖的代價非常昂貴,所以鎖定的程式碼範圍應該儘可能小,只要鎖住資源讀寫部分的程式碼即可。使用互斥鎖也會影響併發的目的。

1.7.2 NSLock

- (void)testNSLock {
    NSLock *lock = [[NSLock alloc] init];
    [lock lock];
    // 需要鎖定的程式碼
    [lock unlock];
}
複製程式碼

1.7.3 atomic 原子屬性

OC在定義屬性時有nonatomic和atomic兩種選擇。 atomic:原子屬性,為setter方法加鎖(預設就是atomic) nonatomic:非原子屬性,不會為setter方法加鎖。 atomic加鎖原理:

 @property (assign, atomic) int age;
 - (void)setAge:(int)age
 { 
     @synchronized(self) { 
        _age = age;
     }
 }
複製程式碼

atomic:執行緒安全,需要消耗大量的資源 nonatomic:非執行緒安全,適合記憶體小的移動裝置= iOS開發的建議: (1)所有屬性都宣告為nonatomic (2)儘量避免多執行緒搶奪同一塊資源 (3)儘量將加鎖、資源搶奪的業務邏輯交給伺服器端處理,減小移動客戶端的壓力

1.7.4 dispatch_semaphore_t 訊號量

- (void)testSemaphone {
    dispatch_semaphore_t semaphore_t = dispatch_semaphore_create(1);
    /// 執行緒1
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        /// 進入等待狀態!
        dispatch_semaphore_wait(semaphore_t, DISPATCH_TIME_FOREVER);
        sleep(7);
        dispatch_semaphore_signal(semaphore_t);
    });
}

複製程式碼

其他的不常用的加鎖操作不再贅述。

執行緒鎖相關參考文章:

深入理解iOS開發中的鎖 iOS 中幾種常用的鎖總結 iOS多執行緒-各種執行緒鎖的簡單介紹

1.8 執行緒間通訊

//在主執行緒上執行操作,例如給UIImageVIew設定圖片
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
//在指定執行緒上執行操作
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait
複製程式碼

2. 多執行緒實現方案

多執行緒實現方案

2.1 NSThread

- (void)testNSThread {
    /// 獲取當前執行緒
    NSThread *currentThread = [NSThread currentThread];
    
    /// 建立需要自己啟動的執行緒
    NSThread *creatThread = [[NSThread alloc] initWithTarget:self selector:@selector(runMethod) object:nil];
    [creatThread start];

    /// 建立自動啟動的執行緒
    [NSThread detachNewThreadSelector:@selector(runMethod2) toTarget:self withObject:nil];
}
- (void)runMethod {
    NSLog(@"runMethod ++ %@",[NSThread currentThread]);
}
- (void)runMethod2 {
    NSLog(@"runMethod2 ++ %@",[NSThread currentThread]);
}
複製程式碼
// 獲取當前執行緒
 + (NSThread *)currentThread;
 // 建立啟動執行緒
 + (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;
 // 判斷是否是多執行緒
 + (BOOL)isMultiThreaded;
 // 執行緒休眠 NSDate 休眠到什麼時候
 + (void)sleepUntilDate:(NSDate *)date;
 // 執行緒休眠時間
 + (void)sleepForTimeInterval:(NSTimeInterval)ti;
 // 結束/退出當前執行緒
 + (void)exit;
 // 獲取當前執行緒優先順序
 + (double)threadPriority;
 // 設定執行緒優先順序 預設為0.5 取值範圍為0.0 - 1.0 
 // 1.0優先順序最高
 // 設定優先順序
 + (BOOL)setThreadPriority:(double)p;
 // 獲取指定執行緒的優先順序
 - (double)threadPriority NS_AVAILABLE(10_6, 4_0);
 - (void)setThreadPriority:(double)p NS_AVAILABLE(10_6, 4_0);
 // 設定執行緒的名字
 - (void)setName:(NSString *)n NS_AVAILABLE(10_5, 2_0);
 - (NSString *)name NS_AVAILABLE(10_5, 2_0);
 // 判斷指定的執行緒是否是 主執行緒
 - (BOOL)isMainThread NS_AVAILABLE(10_5, 2_0);
 // 判斷當前執行緒是否是主執行緒
 + (BOOL)isMainThread NS_AVAILABLE(10_5, 2_0); // reports whether current thread is main
 // 獲取主執行緒
 + (NSThread *)mainThread NS_AVAILABLE(10_5, 2_0);
 - (id)init NS_AVAILABLE(10_5, 2_0);    // designated initializer
 // 建立執行緒
 - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument NS_AVAILABLE(10_5, 2_0);
 // 指定執行緒是否在執行
 - (BOOL)isExecuting NS_AVAILABLE(10_5, 2_0);
 // 執行緒是否完成
 - (BOOL)isFinished NS_AVAILABLE(10_5, 2_0);
 // 執行緒是否被取消 (是否給當前執行緒發過取消訊號)
 - (BOOL)isCancelled NS_AVAILABLE(10_5, 2_0);
 // 傳送執行緒取消訊號的 最終執行緒是否結束 由 執行緒本身決定
 - (void)cancel NS_AVAILABLE(10_5, 2_0);
 // 啟動執行緒
 - (void)start NS_AVAILABLE(10_5, 2_0);
 // 執行緒主函式  線上程中執行的函式 都要在-main函式中呼叫,自定義執行緒中重寫-main方法
 - (void)main NS_AVAILABLE(10_5, 2_0);    // thread body metho
複製程式碼

2.2 GCD 實現多執行緒

首先關於同步,非同步,並行,序列,一張圖便可說清楚。

同步非同步

文字版:

dispatch :派遣/排程
queue:佇列
    用來存放任務的先進先出(FIFO)的容器
sync:同步
    只是在當前執行緒中執行任務,不具備開啟新執行緒的能力
async:非同步
    可以在新的執行緒中執行任務,具備開啟新執行緒的能力
concurrent:併發
    多個任務併發(同時)執行
序列:
    一個任務執行完畢後,再執行下一個任務
複製程式碼

2.2.1 任務

 - queue:佇列
 - block:任務
// 1.用同步的方式執行任務
dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);

// 2.用非同步的方式執行任務
dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

// 3.GCD中還有個用來執行任務的函式
// 在前面的任務執行結束後它才執行,而且它後面的任務等它執行完成之後才會執行
dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
複製程式碼

2.2.2 佇列

  • 序列佇列:序列佇列一次只排程一個任務,一個任務完成後再排程下一個任務。
// 1.使用dispatch_queue_create函式建立序列佇列
// 建立序列佇列(佇列型別傳遞NULL或者DISPATCH_QUEUE_SERIAL)
dispatch_queue_t queue = dispatch_queue_create("佇列名稱", NULL);

// 2.使用dispatch_get_main_queue()獲得主佇列
dispatch_queue_t queue = dispatch_get_main_queue();
注意:主佇列是GCD自帶的一種特殊的序列佇列,放在主佇列中的任務,都會放到主執行緒中執行。
複製程式碼
  • 併發佇列:併發佇列可以同時排程多個任務,排程任務的方式,取決於執行任務的函式;併發功能只有在非同步的(dispatch_async)函式下才有效;非同步狀態下,開啟的執行緒上線由GCD底層決定。
// 1.使用dispatch_queue_create函式建立佇列
dispatch_queue_t

//引數一: 佇列名稱,該名稱可以協助開發除錯以及崩潰分析報告 
//引數二: 佇列的型別
dispatch_queue_create(const char * _Nullable label, dispatch_queue_attr_t  _Nullable attr);

// 2.建立併發佇列
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);

// 執行緒中通訊常用:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // 耗時操作
    // ...
    //放回主執行緒的函式
    dispatch_async(dispatch_get_main_queue(), ^{
        // 在主執行緒更新 UI
    });
});
複製程式碼
  • 全域性併發佇列
//使用dispatch_get_global_queue函式獲得全域性的併發佇列
dispatch_queue_t dispatch_get_global_queue(dispatch_queue_priority_t priority, unsigned long flags);
// dispatch_queue_priority_t priority(佇列的優先順序 )
// unsigned long flags( 此引數暫時無用,用0即可 )

//獲得全域性併發佇列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
複製程式碼

全域性併發佇列的優先順序:

//全域性併發佇列的優先順序
#define DISPATCH_QUEUE_PRIORITY_HIGH 2 // 高優先順序
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 // 預設(中)優先順序
//注意,自定義佇列的優先順序都是預設優先順序
#define DISPATCH_QUEUE_PRIORITY_LOW (-2) // 低優先順序
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN // 後臺優先順序
複製程式碼

2.2.3 GCD 的其他用法

  • 延時執行
dispatch_after(3.0, dispatch_get_main_queue(), ^{
   /// 延時3秒執行的操作!
});
複製程式碼
  • 一次性執行
// 使用dispatch_once函式能保證某段程式碼在程式執行過程中只被執行1次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    // 只執行1次的程式碼(這裡面預設是執行緒安全的)
});
複製程式碼
  • 排程組(佇列組)
//建立排程組
dispatch_group_t group = dispatch_group_create();
//將排程組新增到佇列,執行 block 任務
dispatch_group_async(group, queue, block);
//當排程組中的所有任務執行結束後,獲得通知,統一做後續操作
dispatch_group_notify(group, dispatch_get_main_queue(), block);
複製程式碼

例如:

// 分別非同步執行2個耗時的操作、2個非同步操作都執行完畢後,再回到主執行緒執行操作
dispatch_group_t group =  dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // 執行1個耗時的非同步操作
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // 執行1個耗時的非同步操作
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    // 等前面的非同步操作都執行完畢後,回到主執行緒...
});
複製程式碼
  • GCD 定時器
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0, DISPATCH_TARGET_QUEUE_DEFAULT);
dispatch_source_set_event_handler(source, ^(){
     NSLog(@"Time flies.");
});
dispatch_time_t start
dispatch_source_set_timer(source, DISPATCH_TIME_NOW, 5ull * NSEC_PER_SEC,100ull * NSEC_PER_MSEC);
self.source = source;
dispatch_resume(self.source);
複製程式碼

2.2.4 GCD 自定義封裝工具類

XWGCDManager in Github

//
//  XWGCDManager.h
//  XWGCDManager
//
//  Created by 邱學偉 on 2017/3/3.
//  Copyright © 2017年 邱學偉. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "XWGCDGroup.h"
@interface XWGCDManager : NSObject
/// 主執行緒執行
+ (void)executeInMainQueue:(dispatch_block_t)block;
/// 預設非同步執行緒執行
+ (void)executeInGlobalQueue:(dispatch_block_t)block;
/// 高優先順序非同步執行緒執行
+ (void)executeInHighPriorityGlobalQueue:(dispatch_block_t)block;
/// 低優先順序非同步執行緒執行
+ (void)executeInLowPriorityGlobalQueue:(dispatch_block_t)block;
/// 後臺優先順序非同步執行緒執行
+ (void)executeInBackgroundPriorityGlobalQueue:(dispatch_block_t)block;
/// 主執行緒延時執行
+ (void)executeInMainQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 預設非同步執行緒延時執行
+ (void)executeInGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 高優先順序非同步執行緒延時執行
+ (void)executeInHighPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 低優先順序非同步執行緒延時執行
+ (void)executeInLowPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 後臺優先順序非同步執行緒延時執行
+ (void)executeInBackgroundPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 當前是否在主執行緒
+ (BOOL)isMainQueue;
/// 線上程組新增非同步任務
- (void)execute:(dispatch_block_t)block inGroup:(XWGCDGroup *)group;
/// 監聽某非同步執行緒組中操作完成執行任務
- (void)notify:(dispatch_block_t)block inGroup:(XWGCDGroup *)group;

+ (XWGCDManager *)mainQueue;
+ (XWGCDManager *)globalQueue;
+ (XWGCDManager *)highPriorityGlobalQueue;
+ (XWGCDManager *)lowPriorityGlobalQueue;
+ (XWGCDManager *)backgroundPriorityGlobalQueue;
@end

複製程式碼

2.3 NSOperation 實現多執行緒

NSOperation是基於GCD的物件導向的使用OC語言的封裝。相比GCD,NSOperation的使用更加簡單。NSOperation 是一個抽象類,也就是說它並不能直接使用,而是應該使用它的子類。使用它的子類的方法有三種,使用蘋果為我們提供的兩個子類 NSInvocationOperationNSBlockOperation 和自定義繼承自NSOperation的子類。

NSOperation的使用常常是配合NSOperationQueue來進行的。只要是使用 NSOperation 的子類建立的例項就能新增到 NSOperationQueue 操作佇列之中,一旦新增到佇列,操作就會自動非同步執行(注意是非同步)。如果沒有新增到佇列,而是使用 start 方法,則會在當前執行緒執行。

我們知道,執行緒間的通訊主要是主執行緒與分執行緒之間進行的。主執行緒到分執行緒,NSOperation子類也有相應帶引數的方法;而分執行緒到主執行緒,比如更新UI,它也有很方便的獲取主佇列(被新增到主佇列的操作預設會在主執行緒執行)的方法:[NSOperationQueue mainQueue]

2.3.1 NSInvocationOperation

在當前執行緒中執行:

- (void)testNSOperation {
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadMethod1:) object:@"url"];
    [operation1 start];
}
- (void)downloadMethod1:(id)obj {
    NSLog(@"object: %@ ++ 當前執行緒: %@",obj,[NSThread currentThread]);
}
複製程式碼

執行結果:

(lldb) po [obj class]
__NSCFConstantString

2018-05-15 10:45:09.827562+0800 XWThreadDemo[3148:59049] object: url ++ 當前執行緒: <NSThread: 0x608000072600>{number = 1, name = main}
複製程式碼

在非同步執行緒中執行:

- (void)testNSOperation {
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadMethod1:) object:@"url"];
    NSOperationQueue *queue1 = [[NSOperationQueue alloc] init];
    [queue1 addOperation:operation1];
}
- (void)downloadMethod1:(id)obj {
    NSLog(@"object: %@ ++ 當前執行緒: %@",obj,[NSThread currentThread]);
}
複製程式碼

執行結果:

2018-05-15 10:47:15.889087+0800 XWThreadDemo[3226:62634] object: url ++ 當前執行緒: <NSThread: 0x60800027cb80>{number = 3, name = (null)}
複製程式碼

2.3.2 NSBlockOperation

在不同非同步執行緒新增多個執行方法

- (void)testNSOperation1 {
    NSLog(@"開始");
    /// 建立操作佇列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    for (int i = 0; i < 10; i++) {
        /// 非同步操作
        NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"執行緒:%@,  index: %d",[NSThread currentThread],i);
        }];
        /// 新增到佇列中自動非同步執行
        [queue addOperation:blockOperation];
    }
    NSLog(@"結束");
}
複製程式碼

執行結果:

2018-05-15 10:52:09.662844+0800 XWThreadDemo[3368:69422] 開始
2018-05-15 10:52:09.663440+0800 XWThreadDemo[3368:69536] 執行緒:<NSThread: 0x604000478f80>{number = 4, name = (null)},  index: 2
2018-05-15 10:52:09.663441+0800 XWThreadDemo[3368:69540] 執行緒:<NSThread: 0x600000269a80>{number = 3, name = (null)},  index: 0
2018-05-15 10:52:09.663450+0800 XWThreadDemo[3368:69422] 結束
2018-05-15 10:52:09.663468+0800 XWThreadDemo[3368:69535] 執行緒:<NSThread: 0x60c00007f980>{number = 5, name = (null)},  index: 3
2018-05-15 10:52:09.663470+0800 XWThreadDemo[3368:69534] 執行緒:<NSThread: 0x604000479040>{number = 6, name = (null)},  index: 1
2018-05-15 10:52:09.663514+0800 XWThreadDemo[3368:69533] 執行緒:<NSThread: 0x600000269ac0>{number = 7, name = (null)},  index: 4
2018-05-15 10:52:09.663534+0800 XWThreadDemo[3368:69548] 執行緒:<NSThread: 0x600000269a40>{number = 8, name = (null)},  index: 5
2018-05-15 10:52:09.663547+0800 XWThreadDemo[3368:69549] 執行緒:<NSThread: 0x604000479000>{number = 9, name = (null)},  index: 6
2018-05-15 10:52:09.663566+0800 XWThreadDemo[3368:69550] 執行緒:<NSThread: 0x600000269a00>{number = 10, name = (null)},  index: 7
2018-05-15 10:52:09.663613+0800 XWThreadDemo[3368:69551] 執行緒:<NSThread: 0x608000272900>{number = 11, name = (null)},  index: 8
2018-05-15 10:52:09.663616+0800 XWThreadDemo[3368:69552Test Case '-[XWThreadDemoTests testNSOperation1]' passed (0.002 seconds).
] 執行緒:<NSThread: 0x600000269b80>{number = 12, name = (null)},  index: 9
複製程式碼
  • 使用NSBlockOperation的語法糖
- (void)testNSOperation2 {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperationWithBlock:^{
        NSLog(@"非同步執行");
    }];
}
複製程式碼
  • 執行緒中通訊:
- (void)testNSOperation3 {
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperationWithBlock:^{
        NSLog(@"非同步執行");
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            NSLog(@"回到主執行緒中執行!");
        }];
    }];
}
複製程式碼

2.3.3 NSOperationQueue 的一些高階操作

1. 最大併發數
queue.maxConcurrentOperationCount = 2;
複製程式碼
2. 新增執行緒依賴
- (void)testNSOperationDepend {
    /// 定義三個非同步操作
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        sleep(1);
        NSLog(@"operation1 - 當前執行緒:%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        sleep(5);
        NSLog(@"operation2 - 當前執行緒:%@",[NSThread currentThread]);
    }];
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        sleep(3);
        NSLog(@"operation3 - 當前執行緒:%@",[NSThread currentThread]);
    }];
    /// 定義主執行緒更新UI操作
    NSBlockOperation *operationMain = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"operationMain - 更新UI - 當前執行緒:%@",[NSThread currentThread]);
    }];
    
    /// 新增依賴
    [operation1 addDependency:operation3];
    [operation1 addDependency:operation2];
    [operationMain addDependency:operation3];
    
    /// 非同步執行緒新增非同步佇列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperations:@[operation1,operation2,operation3] waitUntilFinished:YES];
    
    /// 重新整理UI新增主執行緒佇列
    [[NSOperationQueue mainQueue] addOperation:operationMain];
}
複製程式碼

輸出:

Test Case '-[XWThreadDemoTests testNSOperationDepend]' started.
2018-05-15 11:10:44.389619+0800 XWThreadDemo[3825:89159] operation3 - 當前執行緒:<NSThread: 0x608000265f00>{number = 3, name = (null)}
2018-05-15 11:10:46.386336+0800 XWThreadDemo[3825:89156] operation2 - 當前執行緒:<NSThread: 0x60400026a840>{number = 4, name = (null)}
2018-05-15 11:10:47.389426+0800 XWThreadDemo[3825:89156] operation1 - 當前執行緒:<NSThread: 0x60400026a840>{number = 4, name = (null)}
2018-05-15 11:10:47.394948+0800 XWThreadDemo[3825:89109] operationMain - 更新UI - 當前執行緒:<NSThread: 0x60c0000796c0>{number = 1, name = main}
複製程式碼
3. 執行緒掛起
- (void)testNSOperationSuspended {
    //判斷操作的數量,當前佇列裡面是不是有操作?
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    if (queue.operationCount == 0) {
        NSLog(@"當前佇列沒有操作");
        return;
    }
    
    queue.suspended = !queue.isSuspended;
    if (queue.suspended) {
        NSLog(@"暫停");
        
    }else{
        NSLog(@"繼續");
    }
}
複製程式碼

暫停繼續(對佇列的暫停和繼續),掛起的是佇列,不會影響已經在執行的操作

4. 取消佇列中所有操作
- (void)testNSOperationCancel {
    //只能取消所有佇列的裡面的操作,正在執行的無法取消
    //取消操作並不會影響佇列的掛起狀態
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue cancelAllOperations];
    NSLog(@"取消佇列裡所有的操作");
    //取消佇列的掛起狀態
    //(只要是取消了佇列的操作,我們就把佇列處於不掛起狀態,以便於後續的開始)
    queue.suspended = NO;
}
複製程式碼

取消所有佇列的裡面的操作,正在執行的無法取消

3 多執行緒實戰

3.1 輸出一百萬個數字中最大值和最小值

  • pthread
//
//  main.m
//  XWThreadDemo
//
//  Created by 邱學偉 on 2018/5/14.
//  Copyright © 2018年 邱學偉. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import <pthread.h>

struct threadInfo {
    uint32_t * inputValues;
    size_t count;
};

struct threadResult {
    uint32_t min;
    uint32_t max;
};

void * findMinAndMax(void *arg) {
    struct threadInfo const * const info = (struct threadInfo *) arg;
    uint32_t min = UINT32_MAX;
    uint32_t max = 0;
    for (size_t i = 0; i < info -> count; i++) {
        uint32_t value = info -> inputValues[i];
        min = MIN(min, value);
        max = MAX(max, value);
    }
    free(arg);
    struct threadResult * const result = (struct threadResult *) malloc(sizeof( * result));
    result -> min = min;
    result -> max = max;
    return result;
}

int main(int argc, char * argv[]) {
        size_t const count = 100000;
        uint32_t inputValues[count];
        // 填充隨機數字
        for (size_t i = 0; i < count; i++) {
            inputValues[i] = arc4random();
        }
        
        // 開啟4個尋找最大最小值的執行緒
        size_t threadCount = 4;
        pthread_t threads[threadCount];
        for (size_t i = 0; i < threadCount; i++) {
            struct threadInfo * const info = (struct threadInfo *)malloc(sizeof(*info));
            size_t offset = (count / threadCount) * i;
            info -> inputValues = inputValues + offset;
            info -> count = MIN(count - offset, count / threadCount);
            int error = pthread_create(threads + i, NULL, &findMinAndMax, info);
            NSCAssert(error == 0, @"pthread_create() failed: %d", error);
        }
        
        // 等待執行緒退出
        struct threadResult * results[threadCount];
        for (size_t i = 0; i < threadCount; i++) {
            int error = pthread_join(threads[i], (void **) &(results[i]));
            NSCAssert(error == 0, @"pthread_join() failed: %d", error);
        }
        
        // 尋找min 和 max
        uint32_t min = UINT32_MAX;
        uint32_t max = 0;
        for (size_t i = 0; i < threadCount; i++) {
            min = MIN(min, results[i] -> min);
            max = MAX(max, results[i] -> max);
            free(results[i]);
            results[i] = NULL;
        }
        NSLog(@"最小值: %u",min);
        NSLog(@"最大值: %u",max);
    return 0;
}

複製程式碼

輸出:

2018-05-15 14:04:54.347292+0800 XWThreadDemo[8078:249234] 最小值: 30715
2018-05-15 14:04:54.348308+0800 XWThreadDemo[8078:249234] 最大值: 4294961465
複製程式碼
  • NSThread
//
//  ViewController.m
//  XWThreadDemo
//
//  Created by 邱學偉 on 2018/5/14.
//  Copyright © 2018年 邱學偉. All rights reserved.
//

#import "ViewController.h"

@interface XWFindMinAndMaxThread : NSThread
@property (nonatomic, assign) NSUInteger min;
@property (nonatomic, assign) NSUInteger max;
- (instancetype)initWithNumbers:(NSArray <NSNumber *>*)numbers;
@end
@implementation XWFindMinAndMaxThread {
    NSArray <NSNumber *> *_numbers;
}
- (instancetype)initWithNumbers:(NSArray *)numbers {
    if (self = [super init]) {
        _numbers = numbers;
        [self work];
    }
    return self;
}
- (void)work {
    NSUInteger max = 0;
    NSUInteger min = NSUIntegerMax;
    for (NSNumber *number in _numbers) {
        max = MAX(max, number.unsignedIntegerValue);
        min = MIN(min, number.unsignedIntegerValue);
    }
    self.min = min;
    self.max = max;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    
    NSMutableArray *numberArrayM = [NSMutableArray array];
    NSUInteger count = 100000;
    /// 模擬10000個隨機數
    for (NSUInteger i = 0; i < count; i++) {
        [numberArrayM addObject:[NSNumber numberWithUnsignedInteger:arc4random()]];
    }
    
    NSMutableArray <XWFindMinAndMaxThread *> *threads = [NSMutableArray array];
    NSUInteger threadCount = 4;
    NSUInteger numberCount = numberArrayM.count;
    
    for (NSUInteger i = 0; i < threadCount; i++) {
        NSUInteger offset = (numberCount / threadCount) * i;
        NSUInteger count = MIN(numberCount - offset, numberCount / threadCount);
        NSRange range = NSMakeRange(offset, count);
        NSArray *subSet = [numberArrayM subarrayWithRange:range];
        XWFindMinAndMaxThread *findThread = [[XWFindMinAndMaxThread alloc] initWithNumbers:subSet];
        [threads addObject:findThread];
        [findThread start];
    }
    
    NSUInteger max = 0;
    NSUInteger min = NSUIntegerMax;
    for (NSUInteger i = 0; i < threadCount; i++) {
        max = MAX(max, threads[i].max);
        min = MIN(min, threads[i].min);
    }
    
    NSLog(@"最小值: %lu",(unsigned long)min);
    NSLog(@"最大值: %lu",(unsigned long)max);
}
@end

複製程式碼

輸出:

2018-05-15 14:50:51.106993+0800 XWThreadDemo[9540:301745] 最小值: 13300
2018-05-15 14:50:51.107075+0800 XWThreadDemo[9540:301745] 最大值: 4294951952
複製程式碼

3.2 使用 Dispatch Barrier 解決多執行緒併發讀寫同一個資源發生死鎖

- (void)testBarrierSetCount:(NSUInteger)count forKey:(NSString *)key {
    key = [key copy];
    dispatch_queue_t queue = dispatch_queue_create([@"BarrierQueue" UTF8String], DISPATCH_QUEUE_CONCURRENT);
    dispatch_barrier_async(queue, ^{
        if (count == 0) {
            [self.dictM removeObjectForKey:key];
        }else{
            [self.dictM setObject:@(count) forKey:key];
        }
    });
}
複製程式碼

3.3 使用 dispatch_apply 實現效率更高的for迴圈

  • 普通 for 迴圈
- (void)testCommonFor {
    NSLog(@"普通for迴圈開啟");
    NSUInteger max = 10000;
    for (NSUInteger i = 0; i < max; i++) {
        for (NSUInteger j = 0; j < max; j++) {
            /// 執行某操作
        }
    }
    NSLog(@"普通for迴圈結束");
}
複製程式碼

執行時間:21.762 秒

Test Case '-[XWThreadDemoTests testCommonFor]' started.
2018-05-15 17:14:20.215454+0800 XWThreadDemo[23816:496201] 普通for迴圈開啟
2018-05-15 17:14:41.976168+0800 XWThreadDemo[23816:496201] 普通for迴圈結束
Test Case '-[XWThreadDemoTests testCommonFor]' passed (21.762 seconds).
複製程式碼
  • 迴圈
- (void)testApplyFor {
    NSLog(@" dispatch_apply 迴圈開啟");
    size_t max = 100000;
    dispatch_queue_t queue = dispatch_queue_create([@"dispatch_apply" UTF8String], DISPATCH_QUEUE_CONCURRENT);
    dispatch_apply(max, queue, ^(size_t i) {
        dispatch_apply(max, queue, ^(size_t j) {
            /// 執行某操作
        });
    });
    NSLog(@" dispatch_apply 迴圈結束");
}
複製程式碼

執行時間:7.832 秒

Test Case '-[XWThreadDemoTests testApplyFor]' started.
2018-05-15 17:15:51.990662+0800 XWThreadDemo[23881:498546]  dispatch_apply 迴圈開啟
2018-05-15 17:15:59.821032+0800 XWThreadDemo[23881:498546]  dispatch_apply 迴圈結束
Test Case '-[XWThreadDemoTests testApplyFor]' passed (7.832 seconds).
複製程式碼

dispatch_apply 實現的for迴圈有更高的效率!

3.4 使用 dispatch_group_t 追蹤不同佇列中的不同任務

- (void)testGCDGroup {
    NSArray *urls = @[@"https://user-gold-cdn.xitu.io/2018/5/15/1636343424a373e5?w=1260&h=388&f=png&s=180103",@"https://user-gold-cdn.xitu.io/2018/5/15/163634342473da99?w=698&h=348&f=png&s=105695",@"https://user-gold-cdn.xitu.io/2018/5/15/1636343424a373e5?w=1260&h=388&f=png&s=180103"];
    [self downloadImage:urls completion:^(NSArray<UIImage *> *images) {
        NSLog(@"image數量:%lu - %@",(unsigned long)images.count,images);
    }];
}
- (void)downloadImage:(NSArray <NSString *>*)urls completion:(void(^)(NSArray <UIImage *> *images))completion {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSMutableArray *imagesM = [NSMutableArray array];
        dispatch_group_t group = dispatch_group_create();
        for (NSString *url in urls) {
            if (url.length == 0) {
                continue;
            }
            
            // 開啟下載執行緒->
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                //dispatch_group_enter是通知dispatch group任務開始了,dispatch_group_enter和dispatch_group_leave是成對呼叫,不然程式就崩潰了。
                dispatch_group_enter(group);
                NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
                UIImage *image = [UIImage imageWithData:imageData];
                if (image) {
                    [imagesM addObject:image];
                }
                NSLog(@"當前下載執行緒:%@",[NSThread currentThread]);
                // 保持和dispatch_group_enter配對。通知任務已經完成
                dispatch_group_leave(group);
            });
        }
        // 保持和dispatch_group_enter配對。通知任務已經完成
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        // 這裡可以保證所有圖片任務都完成,然後在main queue里加入完成後要處理的閉包,會在main queue裡執行。
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(imagesM.copy);
            }
        });
    });
}
複製程式碼

執行結果:

2018-05-15 17:34:47.265197+0800 XWThreadDemo[24470:522115] 當前下載執行緒:<NSThread: 0x600000270300>{number = 4, name = (null)}
2018-05-15 17:34:48.738196+0800 XWThreadDemo[24470:522114] 當前下載執行緒:<NSThread: 0x608000269440>{number = 5, name = (null)}
2018-05-15 17:34:49.446782+0800 XWThreadDemo[24470:522111] 當前下載執行緒:<NSThread: 0x604000072c40>{number = 3, name = (null)}
2018-05-15 17:34:59.357622+0800 XWThreadDemo[24470:522022] image數量:3 - (
    "<UIImage: 0x6040000b70a0>, {1260, 388}",
    "<UIImage: 0x6080000b3320>, {1260, 388}",
    "<UIImage: 0x6040000b7220>, {698, 348}"
)
複製程式碼

Demo2

- (void)testGCDGroup2 {
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    __block NSMutableArray <UIImage *> *images = [NSMutableArray array];
    
    dispatch_group_async(group, queue, ^(){
        // 會處理一會
        [images addObject:[self imageWithUrl:@"https://user-gold-cdn.xitu.io/2018/5/15/1636343424a373e5?w=1260&h=388&f=png&s=180103"]];
        NSLog(@"圖片1執行緒 - %@",[NSThread currentThread]);
    });
    dispatch_group_async(group, queue, ^(){
        // 處理一會兒
        [images addObject:[self imageWithUrl:@"https://user-gold-cdn.xitu.io/2018/5/15/163634342473da99?w=698&h=348&f=png&s=105695"]];
        NSLog(@"圖片2執行緒 - %@",[NSThread currentThread]);
    });
    
    // 上面的都搞定後這裡會執行一次
    dispatch_group_notify(group, dispatch_get_main_queue(), ^(){
        NSLog(@"image數量:%lu - %@",(unsigned long)images.count,images);
    });
}
複製程式碼

執行結果:

2018-05-15 17:48:36.616813+0800 XWThreadDemo[26394:540836] 圖片2執行緒 - <NSThread: 0x600000273340>{number = 4, name = (null)}
2018-05-15 17:48:38.395960+0800 XWThreadDemo[26394:540833] 圖片1執行緒 - <NSThread: 0x6040002772c0>{number = 5, name = (null)}
2018-05-15 17:48:38.396442+0800 XWThreadDemo[26394:540711] image數量:2 - (
    "<UIImage: 0x6080002a6a80>, {698, 348}",
    "<UIImage: 0x6040002a38a0>, {1260, 388}"
)
複製程式碼

3.5 drawRect在後臺繪製

drawRect:方法會影響效能,所以可以放到後臺執行。

//使用UIGraphicsBeginImageContextWithOptions取代UIGraphicsGetCurrentContext:方法
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
// drawing code here
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
複製程式碼
專案中演示Demo地址:

XWThreadDemo in Github

相關文章