多線程----NSThread的使用、數(shù)據(jù)安全和通信

今天看了一天小碼哥的視頻,重新理解了一下多線程中的NSThread。希望把自己學(xué)到的和理解的能夠分享給大家。

NSThread的使用方法

當(dāng)線程的 number == 1 的時候說明該線程是主線程,反之為子線程

    // 獲取主線程
    NSThread * mainThread = [NSThread mainThread];
    NSLog(@"主線程---%zd",mainThread);
    
    // 獲取當(dāng)前線程
    NSThread * currentThread = [NSThread currentThread];
    NSLog(@"子線程---%zd",currentThread);

NSThread的三種創(chuàng)建方法

  1. alloc init 創(chuàng)建線程,需要手動啟動線程
     // 1.創(chuàng)建線程
    NSThread * threadA = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];
    
    // 設(shè)置名稱
    threadA.name = @"子線程A";
    // 設(shè)置線程優(yōu)先級  取值范圍 0.0 ~ 1.0 之間 最高1.0  默認(rèn)優(yōu)先級是0.5
    threadA.threadPriority = 0.1;

    // 2.執(zhí)行線程
    [threadA start];
  1. 分離子線程 自動啟動線程
 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"分離子線程"];
  1. 開啟后臺線程
 [self performSelectorInBackground:@selector(run:) withObject:@"開啟后臺線程"];

執(zhí)行方法

- (void) run: (NSString *) param{
    NSLog(@"--------run---------%@---------%@",[NSThread currentThread],param);
}

阻塞線程方法

// 阻塞線程 阻塞時間完成后才會釋放線程
    // 方法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í)行完成后會自動釋放


NSThread的線程安全

互斥鎖

@synchronized (<#token#>) {
        <#statements#>
    }

互斥鎖優(yōu)缺點(diǎn)
優(yōu)點(diǎn):能有效防止因多線程搶奪資源造成的數(shù)據(jù)安全問題
缺點(diǎn):需要消耗大量的CPU資源
使用互斥鎖的前提:多線程搶奪同一塊資源
相關(guān)專業(yè)術(shù)語:線程同步
線程同步:多條線程在同一條線上執(zhí)行(按順序的執(zhí)行任務(wù))

我們模擬了三個售票員同時出售同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í)行方法

如果沒有加互斥鎖的話會導(dǎo)致同一張票被三個售票員同時售出,這種情況我們當(dāng)然是不允許的

- (void) saleTicket{
    
    while (1) {
        // 鎖:必須是全局唯一的 一般使用 self
        // 1.注意加鎖位置
        // 2.注意加鎖的前提條件,多線程共享一塊資源
        // 3.注意加鎖是需要代價的,需要耗費(fèi)性能和時間
        // 4.加鎖的結(jié)果:線程同步  當(dāng)線程A執(zhí)行的時候進(jìn)行加鎖 線程B在外等著線程A結(jié)束 鎖開了執(zhí)行線程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(@"票已經(jīng)賣完");
                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.是因為 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 去調(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]);
}

我是Renjiee 我要做最騷的程序猿????????????

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容