iOS 多線程詳解
Slogan : 可能是最通俗易懂的 iOS多線程 詳細(xì)解析文章
1. 基礎(chǔ)概念
1.1 進(jìn)程
進(jìn)程是計算機(jī)中已運(yùn)行程序的實體,是線程的容器維基百科-進(jìn)程妖胀。每個進(jìn)程之間是相互獨(dú)立的做粤,每個進(jìn)程均運(yùn)行在器專用且收保護(hù)的內(nèi)存空間內(nèi)怕品。
把工廠作為一個系統(tǒng)肉康,進(jìn)程類似于車間涨薪。
1.2 線程
線程是操作系統(tǒng)能夠進(jìn)行運(yùn)算調(diào)度的最小單位維基百科-線程刚夺。一個進(jìn)程的所有任務(wù)都在線程中執(zhí)行侠姑。一個線程中執(zhí)行的任務(wù)是串行的莽红,同一時間內(nèi)1個線程只能執(zhí)行一個任務(wù)。
把工廠作為一個系統(tǒng)鬼店,線程類似于車間里干活的工人薪韩。
1.3 進(jìn)程和線程之間關(guān)系
- 線程是CPU調(diào)用的最小單位
- 進(jìn)程手機(jī)CPU分配資源的最小單位
- 一個進(jìn)程中至少有一個線程
- 同一個進(jìn)程內(nèi)的線程共享進(jìn)程的資源
1.4 多線程
一個進(jìn)程可以開啟多條線程,每條線程可以同時執(zhí)行不同的任務(wù)拉盾,多線程技術(shù)可以提高程序的執(zhí)行效率捉偏。同一時間內(nèi),CPU只能處理1條線程讹躯,只有1條線程在工作潮梯,多線程并發(fā)執(zhí)行耙旦,其實是CPU快速的在多條線程之間調(diào)度免都,如果CPU調(diào)度線程的時間足夠快琴昆,就造成了多線程并發(fā)執(zhí)行的假象抖拦。CPU在多條線程之間調(diào)度會消耗大量的CPU資源态罪,同時每條線程被調(diào)度的頻次會降低绩聘,因此我們只開辟3-5條線程凿菩。
1.5 多線程優(yōu)缺點(diǎn)
優(yōu)點(diǎn):1、能適當(dāng)提高程序的執(zhí)行效率获黔;2玷氏、能適當(dāng)提高資源利用率(CPU,內(nèi)存利用率)
缺點(diǎn): 1、創(chuàng)建線程的開銷耻陕,在iOS中膘怕,內(nèi)核數(shù)據(jù)結(jié)構(gòu)(大約1kb)岛心、椡牛空間(子線程512kb,主線程1MB)創(chuàng)建線程大約需要90毫秒的創(chuàng)建時間干旁,如果開啟大量線程會降低程序性能,線程越多换薄,CPU在調(diào)度線程上的開銷就越大。
1.6 線程的狀態(tài)
- 創(chuàng)建:實例化對象
- 就緒:向線程對象發(fā)送start消息伦腐,線程對象被加入 “可調(diào)度線程池”,等待CPU調(diào)度咳焚,detach 方法 和 performSelectorInBackground 方法會直接實例化一個線程對象并加入 “可調(diào)度線程池”
- 運(yùn)行:CPU 負(fù)責(zé)調(diào)度 “可調(diào)度線程池”中線程的執(zhí)行碑定,線程執(zhí)行完成之前延刘,狀態(tài)可能會在 “就緒” 和 “運(yùn)行” 之間來回切換,此過程CPU控制普泡。
- 阻塞:當(dāng)滿足某個預(yù)定條件時,可以使用休眠或鎖阻塞線程執(zhí)行砰嘁,影響的方法有:sleepForTimeInterval, sleepUntilDate, @synchronized(self) 線程鎖。線程對象進(jìn)入阻塞狀態(tài)后,會被“可調(diào)度線程池” 中移除,CPU不再調(diào)度伏伯。
- 死亡:死亡后線程對象的 isFinished 屬性為YES;如果發(fā)送cancel消息,線程對象的 isCanceled 屬性為YES;死亡后 stackSize == 0, 內(nèi)存空間被釋放弄唧。
1.7 線程鎖的幾種方案
加解鎖速度不表示鎖的效率,只表示加解鎖操作在執(zhí)行時的復(fù)雜程度。
1.7.1 互斥鎖
@synchronized(鎖對象) {
// 需要鎖定的代碼
}
使用互斥鎖麸俘,在同一個時間搂誉,只允許一條線程執(zhí)行鎖中的代碼炭懊。因為互斥鎖的代價非常昂貴侮腹,所以鎖定的代碼范圍應(yīng)該盡可能小,只要鎖住資源讀寫部分的代碼即可稻励。使用互斥鎖也會影響并發(fā)的目的父阻。
1.7.2 NSLock
- (void)testNSLock {
NSLock *lock = [[NSLock alloc] init];
[lock lock];
// 需要鎖定的代碼
[lock unlock];
}
1.7.3 atomic 原子屬性
OC在定義屬性時有nonatomic和atomic兩種選擇。
atomic:原子屬性望抽,為setter方法加鎖(默認(rèn)就是atomic)
nonatomic:非原子屬性加矛,不會為setter方法加鎖煤篙。
atomic加鎖原理:
@property (assign, atomic) int age;
- (void)setAge:(int)age
{
@synchronized(self) {
_age = age;
}
}
atomic:線程安全斟览,需要消耗大量的資源
nonatomic:非線程安全,適合內(nèi)存小的移動設(shè)備=
iOS開發(fā)的建議:
(1)所有屬性都聲明為nonatomic
(2)盡量避免多線程搶奪同一塊資源
(3)盡量將加鎖辑奈、資源搶奪的業(yè)務(wù)邏輯交給服務(wù)器端處理苛茂,減小移動客戶端的壓力
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), ^{
/// 進(jìn)入等待狀態(tài)!
dispatch_semaphore_wait(semaphore_t, DISPATCH_TIME_FOREVER);
sleep(7);
dispatch_semaphore_signal(semaphore_t);
});
}
其他的不常用的加鎖操作不再贅述。
線程鎖相關(guān)參考文章:
深入理解iOS開發(fā)中的鎖
iOS 中幾種常用的鎖總結(jié)
iOS多線程-各種線程鎖的簡單介紹
1.8 線程間通信
//在主線程上執(zhí)行操作鸠窗,例如給UIImageVIew設(shè)置圖片
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
//在指定線程上執(zhí)行操作
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait
2. 多線程實現(xiàn)方案
2.1 NSThread
- (void)testNSThread {
/// 獲取當(dāng)前線程
NSThread *currentThread = [NSThread currentThread];
/// 創(chuàng)建需要自己啟動的線程
NSThread *creatThread = [[NSThread alloc] initWithTarget:self selector:@selector(runMethod) object:nil];
[creatThread start];
/// 創(chuàng)建自動啟動的線程
[NSThread detachNewThreadSelector:@selector(runMethod2) toTarget:self withObject:nil];
}
- (void)runMethod {
NSLog(@"runMethod ++ %@",[NSThread currentThread]);
}
- (void)runMethod2 {
NSLog(@"runMethod2 ++ %@",[NSThread currentThread]);
}
// 獲取當(dāng)前線程
+ (NSThread *)currentThread;
// 創(chuàng)建啟動線程
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;
// 判斷是否是多線程
+ (BOOL)isMultiThreaded;
// 線程休眠 NSDate 休眠到什么時候
+ (void)sleepUntilDate:(NSDate *)date;
// 線程休眠時間
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
// 結(jié)束/退出當(dāng)前線程
+ (void)exit;
// 獲取當(dāng)前線程優(yōu)先級
+ (double)threadPriority;
// 設(shè)置線程優(yōu)先級 默認(rèn)為0.5 取值范圍為0.0 - 1.0
// 1.0優(yōu)先級最高
// 設(shè)置優(yōu)先級
+ (BOOL)setThreadPriority:(double)p;
// 獲取指定線程的優(yōu)先級
- (double)threadPriority NS_AVAILABLE(10_6, 4_0);
- (void)setThreadPriority:(double)p NS_AVAILABLE(10_6, 4_0);
// 設(shè)置線程的名字
- (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);
// 判斷當(dāng)前線程是否是主線程
+ (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
// 創(chuàng)建線程
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument NS_AVAILABLE(10_5, 2_0);
// 指定線程是否在執(zhí)行
- (BOOL)isExecuting NS_AVAILABLE(10_5, 2_0);
// 線程是否完成
- (BOOL)isFinished NS_AVAILABLE(10_5, 2_0);
// 線程是否被取消 (是否給當(dāng)前線程發(fā)過取消信號)
- (BOOL)isCancelled NS_AVAILABLE(10_5, 2_0);
// 發(fā)送線程取消信號的 最終線程是否結(jié)束 由 線程本身決定
- (void)cancel NS_AVAILABLE(10_5, 2_0);
// 啟動線程
- (void)start NS_AVAILABLE(10_5, 2_0);
// 線程主函數(shù) 在線程中執(zhí)行的函數(shù) 都要在-main函數(shù)中調(diào)用妓羊,自定義線程中重寫-main方法
- (void)main NS_AVAILABLE(10_5, 2_0); // thread body metho
2.2 GCD 實現(xiàn)多線程
首先關(guān)于同步,異步稍计,并行躁绸,串行,一張圖便可說清楚臣嚣。
文字版:
dispatch :派遣/調(diào)度
queue:隊列
用來存放任務(wù)的先進(jìn)先出(FIFO)的容器
sync:同步
只是在當(dāng)前線程中執(zhí)行任務(wù)净刮,不具備開啟新線程的能力
async:異步
可以在新的線程中執(zhí)行任務(wù),具備開啟新線程的能力
concurrent:并發(fā)
多個任務(wù)并發(fā)(同時)執(zhí)行
串行:
一個任務(wù)執(zhí)行完畢后茧球,再執(zhí)行下一個任務(wù)
2.2.1 任務(wù)
- queue:隊列
- block:任務(wù)
// 1.用同步的方式執(zhí)行任務(wù)
dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);
// 2.用異步的方式執(zhí)行任務(wù)
dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
// 3.GCD中還有個用來執(zhí)行任務(wù)的函數(shù)
// 在前面的任務(wù)執(zhí)行結(jié)束后它才執(zhí)行庭瑰,而且它后面的任務(wù)等它執(zhí)行完成之后才會執(zhí)行
dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
2.2.2 隊列
- 串行隊列:串行隊列一次只調(diào)度一個任務(wù)渗勘,一個任務(wù)完成后再調(diào)度下一個任務(wù)驼唱。
// 1.使用dispatch_queue_create函數(shù)創(chuàng)建串行隊列
// 創(chuàng)建串行隊列(隊列類型傳遞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自帶的一種特殊的串行隊列,放在主隊列中的任務(wù)辜限,都會放到主線程中執(zhí)行。
- 并發(fā)隊列:并發(fā)隊列可以同時調(diào)度多個任務(wù)穷吮,調(diào)度任務(wù)的方式逻翁,取決于執(zhí)行任務(wù)的函數(shù);并發(fā)功能只有在異步的(dispatch_async)函數(shù)下才有效捡鱼;異步狀態(tài)下八回,開啟的線程上線由GCD底層決定。
// 1.使用dispatch_queue_create函數(shù)創(chuàng)建隊列
dispatch_queue_t
//參數(shù)一: 隊列名稱,該名稱可以協(xié)助開發(fā)調(diào)試以及崩潰分析報告
//參數(shù)二: 隊列的類型
dispatch_queue_create(const char * _Nullable label, dispatch_queue_attr_t _Nullable attr)驾诈;
// 2.創(chuàng)建并發(fā)隊列
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
// 線程中通訊常用:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 耗時操作
// ...
//放回主線程的函數(shù)
dispatch_async(dispatch_get_main_queue(), ^{
// 在主線程更新 UI
});
});
- 全局并發(fā)隊列
//使用dispatch_get_global_queue函數(shù)獲得全局的并發(fā)隊列
dispatch_queue_t dispatch_get_global_queue(dispatch_queue_priority_t priority, unsigned long flags);
// dispatch_queue_priority_t priority(隊列的優(yōu)先級 )
// unsigned long flags( 此參數(shù)暫時無用缠诅,用0即可 )
//獲得全局并發(fā)隊列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
全局并發(fā)隊列的優(yōu)先級:
//全局并發(fā)隊列的優(yōu)先級
#define DISPATCH_QUEUE_PRIORITY_HIGH 2 // 高優(yōu)先級
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 // 默認(rèn)(中)優(yōu)先級
//注意,自定義隊列的優(yōu)先級都是默認(rèn)優(yōu)先級
#define DISPATCH_QUEUE_PRIORITY_LOW (-2) // 低優(yōu)先級
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN // 后臺優(yōu)先級
2.2.3 GCD 的其他用法
- 延時執(zhí)行
dispatch_after(3.0, dispatch_get_main_queue(), ^{
/// 延時3秒執(zhí)行的操作!
});
- 一次性執(zhí)行
// 使用dispatch_once函數(shù)能保證某段代碼在程序運(yùn)行過程中只被執(zhí)行1次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 只執(zhí)行1次的代碼(這里面默認(rèn)是線程安全的)
});
- 調(diào)度組(隊列組)
//創(chuàng)建調(diào)度組
dispatch_group_t group = dispatch_group_create();
//將調(diào)度組添加到隊列乍迄,執(zhí)行 block 任務(wù)
dispatch_group_async(group, queue, block);
//當(dāng)調(diào)度組中的所有任務(wù)執(zhí)行結(jié)束后管引,獲得通知,統(tǒng)一做后續(xù)操作
dispatch_group_notify(group, dispatch_get_main_queue(), block);
例如:
// 分別異步執(zhí)行2個耗時的操作闯两、2個異步操作都執(zhí)行完畢后褥伴,再回到主線程執(zhí)行操作
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 執(zhí)行1個耗時的異步操作
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 執(zhí)行1個耗時的異步操作
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的異步操作都執(zhí)行完畢后,回到主線程...
});
- 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.h
// XWGCDManager
//
// Created by 邱學(xué)偉 on 2017/3/3.
// Copyright ? 2017年 邱學(xué)偉. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "XWGCDGroup.h"
@interface XWGCDManager : NSObject
/// 主線程執(zhí)行
+ (void)executeInMainQueue:(dispatch_block_t)block;
/// 默認(rèn)異步線程執(zhí)行
+ (void)executeInGlobalQueue:(dispatch_block_t)block;
/// 高優(yōu)先級異步線程執(zhí)行
+ (void)executeInHighPriorityGlobalQueue:(dispatch_block_t)block;
/// 低優(yōu)先級異步線程執(zhí)行
+ (void)executeInLowPriorityGlobalQueue:(dispatch_block_t)block;
/// 后臺優(yōu)先級異步線程執(zhí)行
+ (void)executeInBackgroundPriorityGlobalQueue:(dispatch_block_t)block;
/// 主線程延時執(zhí)行
+ (void)executeInMainQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 默認(rèn)異步線程延時執(zhí)行
+ (void)executeInGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 高優(yōu)先級異步線程延時執(zhí)行
+ (void)executeInHighPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 低優(yōu)先級異步線程延時執(zhí)行
+ (void)executeInLowPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 后臺優(yōu)先級異步線程延時執(zhí)行
+ (void)executeInBackgroundPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
/// 當(dāng)前是否在主線程
+ (BOOL)isMainQueue;
/// 在線程組添加異步任務(wù)
- (void)execute:(dispatch_block_t)block inGroup:(XWGCDGroup *)group;
/// 監(jiān)聽某異步線程組中操作完成執(zhí)行任務(wù)
- (void)notify:(dispatch_block_t)block inGroup:(XWGCDGroup *)group;
+ (XWGCDManager *)mainQueue;
+ (XWGCDManager *)globalQueue;
+ (XWGCDManager *)highPriorityGlobalQueue;
+ (XWGCDManager *)lowPriorityGlobalQueue;
+ (XWGCDManager *)backgroundPriorityGlobalQueue;
@end
2.3 NSOperation 實現(xiàn)多線程
NSOperation是基于GCD的面向?qū)ο蟮氖褂肙C語言的封裝漾狼。相比GCD重慢,NSOperation的使用更加簡單。NSOperation
是一個抽象類逊躁,也就是說它并不能直接使用似踱,而是應(yīng)該使用它的子類。使用它的子類的方法有三種志衣,使用蘋果為我們提供的兩個子類 NSInvocationOperation
屯援, NSBlockOperation
和自定義繼承自NSOperation的子類。
NSOperation的使用常常是配合NSOperationQueue來進(jìn)行的念脯。只要是使用 NSOperation
的子類創(chuàng)建的實例就能添加到 NSOperationQueue
操作隊列之中,一旦添加到隊列弯淘,操作就會自動異步執(zhí)行(注意是異步)绿店。如果沒有添加到隊列,而是使用 start
方法庐橙,則會在當(dāng)前線程執(zhí)行假勿。
我們知道,線程間的通信主要是主線程與分線程之間進(jìn)行的态鳖。主線程到分線程转培,NSOperation子類也有相應(yīng)帶參數(shù)的方法;而分線程到主線程浆竭,比如更新UI浸须,它也有很方便的獲取主隊列(被添加到主隊列的操作默認(rèn)會在主線程執(zhí)行)的方法:[NSOperationQueue mainQueue]
惨寿。
2.3.1 NSInvocationOperation
在當(dāng)前線程中運(yùn)行:
- (void)testNSOperation {
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadMethod1:) object:@"url"];
[operation1 start];
}
- (void)downloadMethod1:(id)obj {
NSLog(@"object: %@ ++ 當(dāng)前線程: %@",obj,[NSThread currentThread]);
}
運(yùn)行結(jié)果:
(lldb) po [obj class]
__NSCFConstantString
2018-05-15 10:45:09.827562+0800 XWThreadDemo[3148:59049] object: url ++ 當(dāng)前線程: <NSThread: 0x608000072600>{number = 1, name = main}
在異步線程中運(yùn)行:
- (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: %@ ++ 當(dāng)前線程: %@",obj,[NSThread currentThread]);
}
運(yùn)行結(jié)果:
2018-05-15 10:47:15.889087+0800 XWThreadDemo[3226:62634] object: url ++ 當(dāng)前線程: <NSThread: 0x60800027cb80>{number = 3, name = (null)}
2.3.2 NSBlockOperation
在不同異步線程添加多個執(zhí)行方法
- (void)testNSOperation1 {
NSLog(@"開始");
/// 創(chuàng)建操作隊列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
for (int i = 0; i < 10; i++) {
/// 異步操作
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"線程:%@, index: %d",[NSThread currentThread],i);
}];
/// 添加到隊列中自動異步執(zhí)行
[queue addOperation:blockOperation];
}
NSLog(@"結(jié)束");
}
運(yùn)行結(jié)果:
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] 結(jié)束
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(@"異步執(zhí)行");
}];
}
- 線程中通信:
- (void)testNSOperation3 {
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
NSLog(@"異步執(zhí)行");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"回到主線程中執(zhí)行!");
}];
}];
}
2.3.3 NSOperationQueue 的一些高級操作
1. 最大并發(fā)數(shù)
queue.maxConcurrentOperationCount = 2;
2. 添加線程依賴
- (void)testNSOperationDepend {
/// 定義三個異步操作
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
sleep(1);
NSLog(@"operation1 - 當(dāng)前線程:%@",[NSThread currentThread]);
}];
NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
sleep(5);
NSLog(@"operation2 - 當(dāng)前線程:%@",[NSThread currentThread]);
}];
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
sleep(3);
NSLog(@"operation3 - 當(dāng)前線程:%@",[NSThread currentThread]);
}];
/// 定義主線程更新UI操作
NSBlockOperation *operationMain = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"operationMain - 更新UI - 當(dāng)前線程:%@",[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 - 當(dāng)前線程:<NSThread: 0x608000265f00>{number = 3, name = (null)}
2018-05-15 11:10:46.386336+0800 XWThreadDemo[3825:89156] operation2 - 當(dāng)前線程:<NSThread: 0x60400026a840>{number = 4, name = (null)}
2018-05-15 11:10:47.389426+0800 XWThreadDemo[3825:89156] operation1 - 當(dāng)前線程:<NSThread: 0x60400026a840>{number = 4, name = (null)}
2018-05-15 11:10:47.394948+0800 XWThreadDemo[3825:89109] operationMain - 更新UI - 當(dāng)前線程:<NSThread: 0x60c0000796c0>{number = 1, name = main}
3. 線程掛起
- (void)testNSOperationSuspended {
//判斷操作的數(shù)量,當(dāng)前隊列里面是不是有操作删窒?
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
if (queue.operationCount == 0) {
NSLog(@"當(dāng)前隊列沒有操作");
return;
}
queue.suspended = !queue.isSuspended;
if (queue.suspended) {
NSLog(@"暫停");
}else{
NSLog(@"繼續(xù)");
}
}
暫停繼續(xù)(對隊列的暫停和繼續(xù))裂垦,掛起的是隊列,不會影響已經(jīng)在執(zhí)行的操作
4. 取消隊列中所有操作
- (void)testNSOperationCancel {
//只能取消所有隊列的里面的操作肌索,正在執(zhí)行的無法取消
//取消操作并不會影響隊列的掛起狀態(tài)
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue cancelAllOperations];
NSLog(@"取消隊列里所有的操作");
//取消隊列的掛起狀態(tài)
//(只要是取消了隊列的操作蕉拢,我們就把隊列處于不掛起狀態(tài),以便于后續(xù)的開始)
queue.suspended = NO;
}
取消所有隊列的里面的操作,正在執(zhí)行的無法取消
3 多線程實戰(zhàn)
3.1 輸出一百萬個數(shù)字中最大值和最小值
- pthread
//
// main.m
// XWThreadDemo
//
// Created by 邱學(xué)偉 on 2018/5/14.
// Copyright ? 2018年 邱學(xué)偉. 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];
// 填充隨機(jī)數(shù)字
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 邱學(xué)偉 on 2018/5/14.
// Copyright ? 2018年 邱學(xué)偉. 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個隨機(jī)數(shù)
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
解決多線程并發(fā)讀寫同一個資源發(fā)生死鎖
- (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
實現(xiàn)效率更高的for循環(huán)
- 普通 for 循環(huán)
- (void)testCommonFor {
NSLog(@"普通for循環(huán)開啟");
NSUInteger max = 10000;
for (NSUInteger i = 0; i < max; i++) {
for (NSUInteger j = 0; j < max; j++) {
/// 執(zhí)行某操作
}
}
NSLog(@"普通for循環(huán)結(jié)束");
}
執(zhí)行時間:21.762 秒
Test Case '-[XWThreadDemoTests testCommonFor]' started.
2018-05-15 17:14:20.215454+0800 XWThreadDemo[23816:496201] 普通for循環(huán)開啟
2018-05-15 17:14:41.976168+0800 XWThreadDemo[23816:496201] 普通for循環(huán)結(jié)束
Test Case '-[XWThreadDemoTests testCommonFor]' passed (21.762 seconds).
- 循環(huán)
- (void)testApplyFor {
NSLog(@" dispatch_apply 循環(huán)開啟");
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) {
/// 執(zhí)行某操作
});
});
NSLog(@" dispatch_apply 循環(huán)結(jié)束");
}
執(zhí)行時間:7.832 秒
Test Case '-[XWThreadDemoTests testApplyFor]' started.
2018-05-15 17:15:51.990662+0800 XWThreadDemo[23881:498546] dispatch_apply 循環(huán)開啟
2018-05-15 17:15:59.821032+0800 XWThreadDemo[23881:498546] dispatch_apply 循環(huán)結(jié)束
Test Case '-[XWThreadDemoTests testApplyFor]' passed (7.832 seconds).
dispatch_apply
實現(xiàn)的for循環(huán)有更高的效率诚亚!
3.4 使用 dispatch_group_t
追蹤不同隊列中的不同任務(wù)
- (void)testGCDGroup {
NSArray *urls = @[@"https://raw.githubusercontent.com/qxuewei/XWCSDNDemos/master/Images/sleepForTimeInterval.png",@"https://raw.githubusercontent.com/qxuewei/XWResources/master/images/threads.png",@"https://raw.githubusercontent.com/qxuewei/XWCSDNDemos/master/Images/sleepForTimeInterval.png"];
[self downloadImage:urls completion:^(NSArray<UIImage *> *images) {
NSLog(@"image數(shù)量:%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任務(wù)開始了晕换,dispatch_group_enter和dispatch_group_leave是成對調(diào)用,不然程序就崩潰了站宗。
dispatch_group_enter(group);
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:imageData];
if (image) {
[imagesM addObject:image];
}
NSLog(@"當(dāng)前下載線程:%@",[NSThread currentThread]);
// 保持和dispatch_group_enter配對闸准。通知任務(wù)已經(jīng)完成
dispatch_group_leave(group);
});
}
// 保持和dispatch_group_enter配對。通知任務(wù)已經(jīng)完成
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
// 這里可以保證所有圖片任務(wù)都完成份乒,然后在main queue里加入完成后要處理的閉包恕汇,會在main queue里執(zhí)行。
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(imagesM.copy);
}
});
});
}
運(yùn)行結(jié)果:
2018-05-15 17:34:47.265197+0800 XWThreadDemo[24470:522115] 當(dāng)前下載線程:<NSThread: 0x600000270300>{number = 4, name = (null)}
2018-05-15 17:34:48.738196+0800 XWThreadDemo[24470:522114] 當(dāng)前下載線程:<NSThread: 0x608000269440>{number = 5, name = (null)}
2018-05-15 17:34:49.446782+0800 XWThreadDemo[24470:522111] 當(dāng)前下載線程:<NSThread: 0x604000072c40>{number = 3, name = (null)}
2018-05-15 17:34:59.357622+0800 XWThreadDemo[24470:522022] image數(shù)量: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://raw.githubusercontent.com/qxuewei/XWCSDNDemos/master/Images/sleepForTimeInterval.png"]];
NSLog(@"圖片1線程 - %@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^(){
// 處理一會兒
[images addObject:[self imageWithUrl:@"https://raw.githubusercontent.com/qxuewei/XWResources/master/images/threads.png"]];
NSLog(@"圖片2線程 - %@",[NSThread currentThread]);
});
// 上面的都搞定后這里會執(zhí)行一次
dispatch_group_notify(group, dispatch_get_main_queue(), ^(){
NSLog(@"image數(shù)量:%lu - %@",(unsigned long)images.count,images);
});
}
運(yùn)行結(jié)果:
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數(shù)量:2 - (
"<UIImage: 0x6080002a6a80>, {698, 348}",
"<UIImage: 0x6040002a38a0>, {1260, 388}"
)
3.5 drawRect在后臺繪制
drawRect:方法會影響性能或辖,所以可以放到后臺執(zhí)行瘾英。
//使用UIGraphicsBeginImageContextWithOptions取代UIGraphicsGetCurrentContext:方法
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
// drawing code here
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();