姓名:謝艾芳? ? 學(xué)號(hào):16040410073
轉(zhuǎn)自http://www.lxweimin.com/p/d1f15dc6e8c1
〖嵌牛導(dǎo)讀〗今天看了一天小碼哥的視頻,重新理解了一下多線程中的NSThread。希望把自己學(xué)到的和理解的能夠分享給大家。
〖嵌牛鼻子〗NSThread的使用、數(shù)據(jù)安全和通信
〖嵌牛提問(wèn)〗如何快速了解NSThread的使用、數(shù)據(jù)安全和通信?
〖嵌牛正文〗
NSThread的使用方法
當(dāng)線程的 number == 1 的時(shí)候說(shuō)明該線程是主線程,反之為子線程
? ? // 獲取主線程
? ? NSThread * mainThread = [NSThread mainThread];
? ? NSLog(@"主線程---%zd",mainThread);
? ?
? ? // 獲取當(dāng)前線程
? ? NSThread * currentThread = [NSThread currentThread];
? ? NSLog(@"子線程---%zd",currentThread);
NSThread的三種創(chuàng)建方法
alloc init? 創(chuàng)建線程,需要手動(dòng)啟動(dòng)線程
? ? // 1.創(chuàng)建線程
? ? NSThread * threadA = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];
? ?
? ? // 設(shè)置名稱(chēng)
? ? threadA.name = @"子線程A";
? ? // 設(shè)置線程優(yōu)先級(jí)? 取值范圍 0.0 ~ 1.0 之間 最高1.0? 默認(rèn)優(yōu)先級(jí)是0.5
? ? threadA.threadPriority = 0.1;
? ? // 2.執(zhí)行線程
? ? [threadA start];
分離子線程 自動(dòng)啟動(dòng)線程
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"分離子線程"];
開(kāi)啟后臺(tái)線程
[self performSelectorInBackground:@selector(run:) withObject:@"開(kāi)啟后臺(tái)線程"];
執(zhí)行方法
- (void) run: (NSString *) param{
? ? NSLog(@"--------run---------%@---------%@",[NSThread currentThread],param);
}
阻塞線程方法
// 阻塞線程 阻塞時(shí)間完成后才會(huì)釋放線程
? ? // 方法1.
//? ? [NSThread sleepForTimeInterval:2.0];
? ? // 方法2.
? ? [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:3.0]];
? ?
? ? NSLog(@"end-----");
強(qiáng)制退出線程方法
- (void) task{
? ? for(NSInteger i = 0; i<100; i++){
? ? ? ? NSLog(@"%zd--------%@",i,[NSThread currentThread]);
? ? ? ?
? ? ? ? if(i == 20){
? ? ? ? ? ? // 強(qiáng)制退出線程
? ? ? ? ? ? [NSThread exit];
? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
生命周期
NSThread的生命周期:當(dāng)線程執(zhí)行完成后會(huì)自動(dòng)釋放
NSThread的線程安全
互斥鎖
@synchronized (<#token#>) {
? ? ? ? <#statements#>
? ? }
互斥鎖優(yōu)缺點(diǎn)
優(yōu)點(diǎn):能有效防止因多線程搶奪資源造成的數(shù)據(jù)安全問(wèn)題
缺點(diǎn):需要消耗大量的CPU資源
使用互斥鎖的前提:多線程搶奪同一塊資源
相關(guān)專(zhuān)業(yè)術(shù)語(yǔ):線程同步
線程同步:多條線程在同一條線上執(zhí)行(按順序的執(zhí)行任務(wù))
我們模擬了三個(gè)售票員同時(shí)出售同100張機(jī)票,方法效果如下
設(shè)置變量
/** 售票員A */
@property (nonatomic, strong) NSThread * threadA;
/** 售票員B */
@property (nonatomic, strong) NSThread * threadB;
/** 售票員C */
@property (nonatomic, strong) NSThread * threadC;
/** 總票數(shù) */
@property (nonatomic, assign) NSInteger totalCount;
創(chuàng)建并執(zhí)行線程
? ? // 設(shè)置總票數(shù)
? ? self.totalCount = 100;
? ?
? ? // 創(chuàng)建線程
? ? 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";
? ?
? ? // 執(zhí)行線程
? ? [self.threadA start];
? ? [self.threadB start];
? ? [self.threadC start];
執(zhí)行方法
如果沒(méi)有加互斥鎖的話會(huì)導(dǎo)致同一張票被三個(gè)售票員同時(shí)售出,這種情況我們當(dāng)然是不允許的
- (void) saleTicket{
? ?
? ? while (1) {
? ? ? ? // 鎖:必須是全局唯一的 一般使用 self
? ? ? ? // 1.注意加鎖位置
? ? ? ? // 2.注意加鎖的前提條件,多線程共享一塊資源
? ? ? ? // 3.注意加鎖是需要代價(jià)的,需要耗費(fèi)性能和時(shí)間
? ? ? ? // 4.加鎖的結(jié)果:線程同步? 當(dāng)線程A執(zhí)行的時(shí)候進(jìn)行加鎖 線程B在外等著線程A結(jié)束 鎖開(kāi)了執(zhí)行線程B
? ? ? ? @synchronized (self) {
? ? ? ? ? ? NSInteger count = self.totalCount;
? ? ? ? ? ?
? ? ? ? ? ? if(count > 0){
? ? ? ? ? ? ? ? // 耗時(shí)操作
? ? ? ? ? ? ? ? for(NSInteger i = 0; i<100000; i++){
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? self.totalCount = count - 1;
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? NSLog(@"%@賣(mài)出一張票,還剩%zd張票",[NSThread currentThread].name,self.totalCount);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? NSLog(@"票已經(jīng)賣(mài)完");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ?
}
NSThread線程間的通信
當(dāng)子線程任務(wù)執(zhí)行完成之后如何返回值給主線程,再由主線程去刷新UI,下面我們由在網(wǎng)上下載一張圖片為栗子
線程間通信的三種方法
/**
? ? * 參數(shù)1. 調(diào)用方法
? ? ? 參數(shù)2. 方法傳遞值
? ? ? 參數(shù)3. 是否完成該方法后執(zhí)行下一步
? ? */
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.省去顯示圖片函數(shù),直接在方法中實(shí)現(xiàn)
? ? // 方法3.
? ? [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
注:方法3.是因?yàn)?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,所以可以直接通過(guò) UIImageView 去調(diào)用方法,從而設(shè)置 UIImageView 的 image --> setImage:
創(chuàng)建線程
? ? 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.根據(jù)url下載圖片二進(jìn)制數(shù)據(jù)到本地
? ? NSData * imageData = [NSData dataWithContentsOfURL:url];
? ?
? ? // 3.轉(zhuǎn)換圖片格式
? ? UIImage * image = [UIImage imageWithData:imageData];
? ?
? ? NSLog(@"download---%@",[NSThread currentThread]);
? ?
? ? // 4.回到主線程顯示UI
? ?
? ? /**
? ? * 參數(shù)1. 調(diào)用方法
? ? ? 參數(shù)2. 方法傳遞值
? ? ? 參數(shù)3. 是否完成該方法后執(zhí)行下一步
? ? */
? ?
? ? // 方法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]);