預(yù)備知識:
1.進程與線程
進程:進程是指在系統(tǒng)中正在運行的一個應(yīng)用程序注祖。每個進程之間是獨立的噪服,每個進程均運行在其專用且受保護的內(nèi)存空間內(nèi)滤钱。(進程的查看可以在活動監(jiān)視器去查看)
線程:1個進程要想執(zhí)行任務(wù)拼弃,必須得有線程略号,每個程序至少要有一條線程官觅。一個進程中所有的任務(wù)都是在線程中執(zhí)行的古劲。如果一個線程有多個任務(wù)需要處理,這就需要一一處理這些任務(wù)缰猴,這就是是線程的串行产艾。
線程與進程的比較:
- 線程是CPU調(diào)用(執(zhí)行任務(wù))的最小單位。
- 進程是CPU分配資源和調(diào)度的單位。
- 一個程序可以對應(yīng)多個進程闷堡,一個進程中可以有多個線程隘膘,但至少要有一個線程。
- 同一個進程內(nèi)的線程共享進程的資源杠览。
2.多線程
多線程:1個進程中可以開啟多條線程弯菊,每條線程可以并行(同時)執(zhí)行不同的任務(wù)。
串行:如果要在1個線程中執(zhí)行多個任務(wù)踱阿,那么只能一個一個地按順序執(zhí)行這些任務(wù)管钳,也就是說,在同一時間內(nèi)软舌,1個線程只能執(zhí)行1個任務(wù)(也就是說線程中同一時間段只能做一件事情)才漆。
并行:多個線程都是執(zhí)行,就是叫并行佛点。
多線程原理(單個CPU的情況下):
同一時間醇滥,CPU只能處理1條線程,只有1條線程在工作(執(zhí)行)超营,多線程并發(fā)(同時)執(zhí)行鸳玩,其實是CPU快速地在多條線程之間調(diào)度(切換),如果CPU調(diào)度線程的時間足夠快演闭,就造成了多線程并發(fā)執(zhí)行的假象不跟。
線程也不是開的越多越好,開得較多的話反而會降低效率米碰,線程一般就3~5條最好躬拢。
3.主線程
主線程:一個iOS程序運行后,默認會開啟1條線程见间,稱為“主線程”或“UI線程”聊闯。
主線程的作用:
- 顯示\刷新UI界面
- 處理UI事件(比如點擊事件、滾動事件米诉、拖拽事件等)
主線程的使用注意:
- 將耗時操作盡量不放在主線程中執(zhí)行菱蔬。(文件上傳等都是耗時操作)
- 耗時操作會卡住主線程,會造成UI界面的卡頓史侣,造成不好的用戶體驗拴泌。
由于在線程中是串行處理的,如果一個耗時操作放在主線程中執(zhí)行惊橱,如果用戶點擊了按鈕蚪腐,而和UI相關(guān)的都是放在主線程的,所以只有當(dāng)耗時操作完成后才會去執(zhí)行按鈕的點擊事件税朴,這樣就會造成卡頓回季,用戶體驗不好家制。
耗時操作優(yōu)化處理的方式:將耗時操作放在子線程(非主線程,后臺線程)中進行處理泡一。
由于子線程和主線程是同時執(zhí)行的颤殴,這樣耗時操作放在子線程中執(zhí)行的,在點擊按鈕的時候鼻忠,就不會造成卡頓的現(xiàn)象出現(xiàn)涵但。
4.多線程的實現(xiàn)方案
- pthread
- (IBAction)btnClickAction:(id)sender {
// 創(chuàng)建線程對象
pthread_t thread;
// 創(chuàng)建線程
/**
創(chuàng)建線程
@param _Nonnull#> 線程對象傳遞地址 description#>
@param _Nullable#> 線程的屬性(優(yōu)先級等) description#>
@param _Nonnull 指向函數(shù)的指針
@return 函數(shù)需要傳遞的函數(shù)
*/
pthread_create(&thread, nil, task, NULL); // 創(chuàng)建一條線程,如果要創(chuàng)建多個線程就可以將上面的代碼copy下就可以了帖蔓。
pthread_t threadB;
pthread_create(&threadB, nil, task, NULL); // 第二個線程
pthread_equal(thread, threadB); // 判斷兩個線程是不是相等矮瘟。
}
void * task(void *parameter) {
NSLog(@"%@ ---- ", [NSThread currentThread]);
// 可以將耗時操作放在這里執(zhí)行。
return NULL;
}
// 打印結(jié)果
2017-08-06 22:47:48.854 1-pthread[4855:328489] <NSThread: 0x608000266d40>{number = 3, name = (null)} ----
2017-08-06 22:47:48.854 1-pthread[4855:328490] <NSThread: 0x608000265b80>{number = 4, name = (null)} ----
- NSThread
創(chuàng)建線程的幾個方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self creatNewThreadMethod];
}
- (void)creatNewThreadMethod {
// 開啟一條后臺線程
[self performSelectorInBackground:@selector(run:) withObject:@"開啟后臺子線程"];
}
- (void)creatDetachNewThreadMethod {
// 創(chuàng)建分離子線程->會自動啟動子線程
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];
}
- (void)creatNewThread {
// 創(chuàng)建線程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];
// 啟動線程塑娇,默認是暫停狀態(tài)的
[thread start];
}
- (void)run:(NSString *)param {
NSLog(@"---run--%@", [NSThread currentThread]);
}
設(shè)置線程的名字以及優(yōu)先級等屬性
// 創(chuàng)建多個線程->給每個線程命名以及優(yōu)先級
- (void)creatMutilThread {
NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];
thread1.name = @"thread 1";
thread1.threadPriority = 0.6; // 設(shè)置優(yōu)先級(0.0~1.0澈侠,默認的優(yōu)先級是0.5)
NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];
thread2.name = @"thread 2";
thread2.threadPriority = 0.3;
NSThread *thread3 = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil];
thread3.name = @"thread 3";
thread3.threadPriority = 1.0;
[thread1 start];
[thread2 start];
[thread3 start];
}
- (void)run:(NSString *)param {
for (int i = 0; i < 10; i++) {
NSLog(@"---run--%@", [NSThread currentThread].name);
}
}
// 打印結(jié)果
2017-08-06 23:11:51.487 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.487 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.487 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.487 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.488 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.487 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.488 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.488 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.488 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.488 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.488 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.488 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.490 2-NSThread基本使用[5136:356390] ---run--thread 3
2017-08-06 23:11:51.489 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.492 2-NSThread基本使用[5136:356388] ---run--thread 1
2017-08-06 23:11:51.493 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.495 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.496 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.496 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.496 2-NSThread基本使用[5136:356389] ---run--thread 2
2017-08-06 23:11:51.497 2-NSThread基本使用[5136:356389] ---run--thread 2
打印次數(shù)越多,打印的數(shù)字就越趨于準(zhǔn)確的值钝吮。
- 線程狀態(tài)
線程的控制狀態(tài)等操作:
// 啟動
- (void)start;
// 阻塞(暫停)線程
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
// 強制停止線程
+ (void)exit; // 一旦線程停止(死亡)了埋涧,就不能再次開啟任務(wù)
// 和break不一致板辽,表示任務(wù)執(zhí)行完畢后才退出的奇瘦。
5.多線程安全隱患
1塊資源可能會被多個線程共享,也就是多個線程可能會訪問同一塊資源劲弦,比如多個線程訪問同一個對象耳标、同一個變量、同一個文件邑跪,當(dāng)多個線程訪問同一塊資源時次坡,很容易引發(fā)數(shù)據(jù)錯亂和數(shù)據(jù)安全問題。
例子:
以上的圖展示存取錢是同時處理的画畅,造成了最終的錢變少了砸琅,造成了數(shù)據(jù)的不當(dāng)處理。
處理的方案
蘋果的文檔說明的解決方案就是加上一個互斥鎖轴踱,在訪問數(shù)據(jù)的時候加上互斥鎖症脂,只要次個線程才能訪問處理,在訪問結(jié)束的時候打開把互斥鎖打開淫僻,然后另一個線程進行訪問诱篷,同樣加上互斥鎖,循環(huán)如此雳灵,這樣就解決了數(shù)據(jù)安全的隱患棕所。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.totalCount = 100;
self.threadA = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
self.threadB = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
self.threadC = [[NSThread alloc] initWithTarget:self selector:@selector(saleTicket) object:nil];
self.threadA.name = @"售票員A";
self.threadB.name = @"售票員B";
self.threadC.name = @"售票員C";
[self.threadA start];
[self.threadB start];
[self.threadC start];
}
- (void)saleTicket {
while (1) {
// 鎖必須是全局唯一的。
// 1.注意點悯辙,不能隨便能亂加琳省,加鎖的位置要注意
// 2.加鎖的前提條件迎吵,--多線程共享同一個資源
// 3.注意加鎖是需要代價的--需要耗費性能的
// 4.加鎖的結(jié)果是造成線程同步。->ABC ABC ABC的循環(huán)執(zhí)行任務(wù)
@synchronized (self) {
NSInteger count = self.totalCount;
if (count > 0) {
self.totalCount = count - 1;
// 賣出去一張票岛啸,還剩下多少張
NSLog(@"%@賣出去一張票, 還剩下%ld張", [NSThread currentThread].name, self.totalCount);
} else {
NSLog(@"沒票了");
break;
}
}
}
}
6.原子和非原子性
在oc中定義屬性時有nonatomic和atomic兩種選擇钓觉;
atomic:原子屬性,為setter方法加鎖(默認就是atomic)坚踩,意味著線程是安全的荡灾。
nonatomic:非原子屬性,不會為setter方法加鎖瞬铸。
7.線程間的通信
有的時候批幌,同一個進程跑了多個線程,有的子線程的輸出的結(jié)果是另一個子線程的輸入嗓节,這就需要兩個子線程中進行一種通信荧缘。所以線程往往不是孤立存在的,多個線程之間需要經(jīng)常進行通信拦宣。
線程之間的通信的方法:
// 回到主線程
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
// 回到線程對象截粗,可以回到主線程也可以回到子線程
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;
例子:下載圖片并展示
如果不做線程的處理,
- (void)wjDownloadImageCountTime {
NSDate *start = [NSDate date]; // 獲得當(dāng)前時間
NSURL *url = [NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/d/573d37cc14e3c.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imageData];
self.imageView.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSDate *end = [NSDate date];
NSLog(@"count time is: %f", [end timeIntervalSinceDate:start]);
}
以上能夠完成圖片的下載鸵隧,圖片的展示也能成功绸罗,但是一旦UI上的圖片比較多的話,就會阻塞主線程豆瘫,所以不采取珊蟀。
優(yōu)化的處理
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 放在子線程去下載圖片
[NSThread detachNewThreadSelector:@selector(wjDownloadImage) toTarget:self withObject:nil];
}
- (void)wjDownloadImage {
// http://img4.imgtn.bdimg.com/it/u=816246739,294523191&fm=214&gp=0.jpg
// 1.下載圖片的url
NSURL *url = [NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/d/573d37cc14e3c.jpg"];
// 2.下載圖片到本地->二進制數(shù)據(jù) -> 耗時操作
NSData *imageData = [NSData dataWithContentsOfURL:url];
// 3.將二進制數(shù)據(jù)轉(zhuǎn)為圖片
UIImage *img = [UIImage imageWithData:imageData];
// 4.顯示UI->回到主線程去刷新UI界面
[self performSelectorOnMainThread:@selector(wjShowImage:) withObject:img waitUntilDone:YES];
// 簡便方法,就不需要寫下面的那個方法了外驱。
// [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:img waitUntilDone:YES];
}
- (void)wjShowImage:(UIImage *)img {
self.imageView.image = img;
}