文藝求關(guān)注.png
是啦传藏,面試的時候大概講出在iOS開發(fā)中用到的幾個多線程之后腻暮,面試官會繼續(xù)詳細咨詢一番,測試你是否有真本事毯侦,接下來西壮,聊聊NSThread如何在開發(fā)中使用
創(chuàng)建和啟動多線程
- 一般來講,一個NSThread對象就代表一條線程
- 創(chuàng)建叫惊、啟動線程
/**
第一種創(chuàng)建NSThread的方法
第一個參數(shù):目標對象
第二個參數(shù):方法選擇器
第三個參數(shù):傳遞給方法選擇器中調(diào)用方法的參數(shù)
*/
// 優(yōu)點:能拿到線程對象款青, 缺點:需要手動的啟動線程
NSThread *thread = [[NSThread alloc] initWithTarget: self selector: @selector(run:) object: @"iOS"];
[thread start];
// 線程一啟動,就會在線程`thread`中執(zhí)行self的`run`方法
- (void) run {
NSLog(@"%@", [NSThread currentThread]); // 答應(yīng)當前線程
}
- 主線程相關(guān)的一些用法
+ (NSThread *) mainThread; // 獲取主線程
- (BOOL) isMainThread; // 是否為主線程
+ (BOOL) isMainThread; // 是否為主線程
- 其他用法
// 設(shè)置線程的名字
thread.name = @"線程A";
[thread setName = @"線程B"];
// 設(shè)置線程的優(yōu)先級 0~1.0 默認0.5霍狰,最高是1.0
thread.threadPriority = 1.0;
- 其他創(chuàng)建NSThread的方法
// 第二種創(chuàng)建NSThread的方法
// 優(yōu)點:自動的啟動線程抡草,缺點:拿不到線程對象
[NSThread detachNewThreadSelector: @selector(run:) toTager: self withObject: nil];
// 第三種創(chuàng)建NSThread的方法——開啟一條后臺線程
// 優(yōu)點:自動啟動線程饰及,缺點:拿不到線程對象
[self performSelectorInBackground: @selector(run:) withObject:@"開啟了一個后臺線程"];
// 第四種創(chuàng)建NSThread的方法——自定義
- 線程的狀態(tài)
NSThread *thread = [[NSThread alloc] initWithTager: self selector: @selector: (run:) object: nil];
[thread start];
線程狀態(tài)示意圖.png
- 控制線程狀態(tài)
// 啟動線程
- (void) start;
// 進入就緒狀態(tài) -> 運行狀態(tài)。 當線程任務(wù)執(zhí)行完畢康震,自動進入死亡狀態(tài)
// 阻塞 (暫停) 線程
+ (void) sleepUntilDate: (NSDate *)date;
+ (void) sleefForTimeInterval: (NSTimeInterval)ti;
// 進入阻塞狀態(tài)
// 強制停止線程
+ (void) exit;
// 進入死亡狀態(tài)
// *注意:一旦線程停止(死亡)燎含,就不能在此開啟任務(wù)
多線程的安全隱患
- 資源共享
- 1塊資源可能會被多個線程共享,也就是
多個線程可能同時會訪問同一塊資源
- ex:多個線程訪問同一個對象腿短、同一個變量屏箍、同一個文件
- 1塊資源可能會被多個線程共享,也就是
- 當多個線程訪問同一塊資源時,很容易引起
數(shù)據(jù)錯亂和數(shù)據(jù)安全
問題
安全隱患分析.png -
安全隱患解決方法——互斥鎖
互斥鎖.png - 互斥鎖使用格式
@synchronized (鎖對象) { // 鎖對象一般是`self`
// 需要鎖定的代碼
// 注意:鎖定1份代碼只用1把鎖橘忱,用多把鎖是無效的
}
- 互斥鎖的優(yōu)缺點
- 優(yōu)點:能有效防止因多線程搶奪資源造成的數(shù)據(jù)安全問題
- 缺點:需要消耗大量的CPU資源
* 互斥鎖的使用前提:多條線程搶奪同一塊資源
- 相關(guān)專業(yè)術(shù)語:
線程同步
- 線程同步的意思是:多條線程在同一條線上執(zhí)行(按順序執(zhí)行任務(wù))
- 互斥鎖赴魁,就是使用線程了線程同步技術(shù)
線程間通信
- 什么叫線程間通信
- 在1個線程中无埃,線程往往不是孤立存在的烂叔,多個線程之間需要經(jīng)常進行通信
- 線程間通信的體現(xiàn)
- 1個線程傳遞數(shù)據(jù)給另一個線程
- 在1個線程中執(zhí)行完特定任務(wù)后,轉(zhuǎn)去另1個線程繼續(xù)執(zhí)行任務(wù)
- 線程間通信常用方法
- (void) performSelectorOnMainThread: (SEL) aSelector withObject: (id)arg waitUntilDone: (BOOL)wait; // 切換至主線程執(zhí)行任務(wù)
- (void) performSelector: (SEL) aSelector onThread: (NSThread *)thr withObject: (id)arg waitUntilDone:(BOOL) wait; // 傳入一個線程執(zhí)行任務(wù) 可以傳入主線程/子線程
案例(在子線程下載圖片嘲更,返回主線程刷新UI)
// 要求:點擊控制器View下載并顯示頁面
@property (nonatomic, weak) UIImageView *imageView;
- (void)touchBegan:(NSSet<UITouch *> *) touches withEvent: (UIEvent *) event {
[NSThread detachNewThreadSelector: @selector(downloadImage) toTarget: self withObject: nil];
}
- (void) downloadImage {
- 01 URL
NSURL *url = [NSURL URLWithString: @""];
- 02 下載圖片的二進制數(shù)據(jù)到本地
NSData *imageData = [NSData dataWithContentsOfURL: url];
- 03 把二進制數(shù)據(jù)轉(zhuǎn)換為圖片
UIImage *image = [UIImage imageWithData:imageData];
NSLog(@"Download---%@", [NSThread currentThread]); // 在子線程中完成圖片下載任務(wù)
- 04 回到主線程設(shè)置圖片
/**
第一種線程間通信(onMainThread)
第一個參數(shù):方法選擇器 回到主線程執(zhí)行的任務(wù)
第二個參數(shù):傳遞給要調(diào)用方法的參數(shù)
第三個參數(shù):是否要等調(diào)用的方法執(zhí)行完畢后才繼續(xù)執(zhí)行后面的任務(wù)
*/
[self performSelectorOnMainThread: @selector(showImage:) withObject:image waitUntilDone:NO];
// 第二種線程間通信(onThread)需要傳入一個線程
[self performSelector: @selector(showImage:) onThread:[NSThread mainThread] withObject: image waitUntilDone:YES];
// *** 特殊:第三種線程間通信(直接傳系統(tǒng)的方法選擇器)
[self performSelector: @selector(setImage:) onThread: [NSThread mainThread] withObject: image waitUntilDone: YES];
}
- (void) showImage: (UIImage *)image {
self.imageView.image = image;
NSLog(@"UI------%@", [NSThread currentThread]); // 主線程刷新UI
}
關(guān)注一下又不會懷孕.png