姓名:謝艾芳? ? 學號:16040410073
轉自http://www.lxweimin.com/p/d1f15dc6e8c1
〖嵌牛導讀〗今天看了一天小碼哥的視頻,重新理解了一下多線程中的NSThread。希望把自己學到的和理解的能夠分享給大家。
〖嵌牛鼻子〗NSThread的使用、數據安全和通信
〖嵌牛提問〗如何快速了解NSThread的使用、數據安全和通信?
〖嵌牛正文〗
NSThread的使用方法
當線程的 number == 1 的時候說明該線程是主線程,反之為子線程
? ? // 獲取主線程
? ? NSThread * mainThread = [NSThread mainThread];
? ? NSLog(@"主線程---%zd",mainThread);
? ?
? ? // 獲取當前線程
? ? NSThread * currentThread = [NSThread currentThread];
? ? NSLog(@"子線程---%zd",currentThread);
NSThread的三種創建方法
alloc init? 創建線程,需要手動啟動線程
? ? // 1.創建線程
? ? NSThread * threadA = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];
? ?
? ? // 設置名稱
? ? threadA.name = @"子線程A";
? ? // 設置線程優先級? 取值范圍 0.0 ~ 1.0 之間 最高1.0? 默認優先級是0.5
? ? threadA.threadPriority = 0.1;
? ? // 2.執行線程
? ? [threadA start];
分離子線程 自動啟動線程
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"分離子線程"];
開啟后臺線程
[self performSelectorInBackground:@selector(run:) withObject:@"開啟后臺線程"];
執行方法
- (void) run: (NSString *) param{
? ? NSLog(@"--------run---------%@---------%@",[NSThread currentThread],param);
}
阻塞線程方法
// 阻塞線程 阻塞時間完成后才會釋放線程
? ? // 方法1.
//? ? [NSThread sleepForTimeInterval:2.0];
? ? // 方法2.
? ? [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3.0]];
? ?
? ? NSLog(@"end-----");
強制退出線程方法
- (void) task{
? ? for(NSInteger i = 0; i<100; i++){
? ? ? ? NSLog(@"%zd--------%@",i,[NSThread currentThread]);
? ? ? ?
? ? ? ? if(i == 20){
? ? ? ? ? ? // 強制退出線程
? ? ? ? ? ? [NSThread exit];
? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
生命周期
NSThread的生命周期:當線程執行完成后會自動釋放
NSThread的線程安全
互斥鎖
@synchronized (<#token#>) {
? ? ? ? <#statements#>
? ? }
互斥鎖優缺點
優點:能有效防止因多線程搶奪資源造成的數據安全問題
缺點:需要消耗大量的CPU資源
使用互斥鎖的前提:多線程搶奪同一塊資源
相關專業術語:線程同步
線程同步:多條線程在同一條線上執行(按順序的執行任務)
我們模擬了三個售票員同時出售同100張機票,方法效果如下
設置變量
/** 售票員A */
@property (nonatomic, strong) NSThread * threadA;
/** 售票員B */
@property (nonatomic, strong) NSThread * threadB;
/** 售票員C */
@property (nonatomic, strong) NSThread * threadC;
/** 總票數 */
@property (nonatomic, assign) NSInteger totalCount;
創建并執行線程
? ? // 設置總票數
? ? 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) {
? ? ? ? // 鎖:必須是全局唯一的 一般使用 self
? ? ? ? // 1.注意加鎖位置
? ? ? ? // 2.注意加鎖的前提條件,多線程共享一塊資源
? ? ? ? // 3.注意加鎖是需要代價的,需要耗費性能和時間
? ? ? ? // 4.加鎖的結果:線程同步? 當線程A執行的時候進行加鎖 線程B在外等著線程A結束 鎖開了執行線程B
? ? ? ? @synchronized (self) {
? ? ? ? ? ? NSInteger count = self.totalCount;
? ? ? ? ? ?
? ? ? ? ? ? if(count > 0){
? ? ? ? ? ? ? ? // 耗時操作
? ? ? ? ? ? ? ? for(NSInteger i = 0; i<100000; i++){
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? self.totalCount = count - 1;
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? NSLog(@"%@賣出一張票,還剩%zd張票",[NSThread currentThread].name,self.totalCount);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? NSLog(@"票已經賣完");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ?
}
NSThread線程間的通信
當子線程任務執行完成之后如何返回值給主線程,再由主線程去刷新UI,下面我們由在網上下載一張圖片為栗子
線程間通信的三種方法
/**
? ? * 參數1. 調用方法
? ? ? 參數2. 方法傳遞值
? ? ? 參數3. 是否完成該方法后執行下一步
? ? */
1.直接回到主線程
? ? // 方法1.
? ? [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
2.回到指定NSThread線程
? ? // 方法2.
? ? [self performSelector:@selector(showImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
3.省去顯示圖片函數,直接在方法中實現
? ? // 方法3.
? ? [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
注:方法3.是因為 UIImageView 繼承的最底層也是NSObject,而方法
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
都是繼承NSObject,所以可以直接通過 UIImageView 去調用方法,從而設置 UIImageView 的 image --> setImage:
創建線程
? ? NSThread * thread = [[NSThread alloc]initWithTarget:self selector:@selector(download) object:nil];
? ?
? ? [thread start];
下載方法
- (void) download{
? ?
? ? // 1.獲取圖片rul
? ? NSURL * url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1502704595&di=77a802f956215c509727a13dc7176b7a&imgtype=jpg&er=1&src=http%3A%2F%2Fatt.bbs.duowan.com%2Fforum%2F201306%2F08%2F220236m63ppvbxbevgrbrg.jpg"];
? ?
? ? // 2.根據url下載圖片二進制數據到本地
? ? NSData * imageData = [NSData dataWithContentsOfURL:url];
? ?
? ? // 3.轉換圖片格式
? ? UIImage * image = [UIImage imageWithData:imageData];
? ?
? ? NSLog(@"download---%@",[NSThread currentThread]);
? ?
? ? // 4.回到主線程顯示UI
? ?
? ? /**
? ? * 參數1. 調用方法
? ? ? 參數2. 方法傳遞值
? ? ? 參數3. 是否完成該方法后執行下一步
? ? */
? ?
? ? // 方法1.
//? ? [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
? ? // 方法2.
//? ? [self performSelector:@selector(showImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
? ? // 方法3.
? ? [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
? ?
? ? NSLog(@"----end----");
}
顯示圖片方法
- (void) showImage: (UIImage *) image{
? ? self.imageView.image = image;
? ? NSLog(@"showImage---%@",[NSThread currentThread]);