iOS開(kāi)發(fā)多線程篇NSThread

示意圖.png

1、優(yōu)缺點(diǎn)

1.優(yōu)點(diǎn):NSThread比其他兩種多線程方案較輕量級(jí),更直觀地控制線程對(duì)象
2.缺點(diǎn):需要自己管理線程的生命周期,線程同步。線程同步對(duì)數(shù)據(jù)的加鎖會(huì)有一定的系統(tǒng)開(kāi)銷

2、 NSThread相關(guān)的主要方法:

創(chuàng)建、啟動(dòng)線程

  // 線程一啟動(dòng),就會(huì)在線程thread中執(zhí)行self的run方法

NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];

[thread start];

創(chuàng)建線程后自動(dòng)啟動(dòng)線程

[NSThread detachNewThreadSelector:@selector(test) toTarget:self withObject:nil];

隱式創(chuàng)建并啟動(dòng)線程

[self performSelectorInBackground:@selector(run) withObject:nil];

3、互斥鎖

@synchronized(鎖對(duì)象) { // 需要鎖定的代碼 }

互斥鎖的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):能有效防止因多線程搶奪資源造成的數(shù)據(jù)安全問(wèn)題
缺點(diǎn):需要消耗大量的CPU資源

互斥鎖的使用前提:多條線程搶奪同一塊資源

4、線程間通信

線程間通信常用方法

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;

5、模擬一個(gè)賣票系統(tǒng)

self.tickets = 100;//總共票數(shù)

NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
thread1.name = @"售票機(jī)1";
[thread1 start];

NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
thread2.name = @"售票機(jī)2";
[thread2 start];



- (void)run {

    if (_lock == nil)
    {
        _lock = [[NSLock alloc] init];
    }

    while (YES)
    {
        [_lock lock];
        if (_tickets > 0)
        {
            _tickets -= 1;
            NSString *message = [NSString stringWithFormat:@"當(dāng)前票數(shù)是%ld,售票線程是%@",(long)_tickets,[[NSThread currentThread] name]];

            NSLog(@"message == %@",message);
        
            [_lock unlock];
        
            if ([[[NSThread currentThread] name] isEqualToString:@"售票機(jī)1"])
            {
                [NSThread sleepForTimeInterval:0.2];
            }
            else
                [NSThread sleepForTimeInterval:0.3];
        }
        else
        {
            [_lock unlock];
            break;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 上篇我們學(xué)習(xí)了iOS多線程解決方式中的NSOperation,這篇我主要概況總結(jié)iOS多線程中NSThread的解...
    碼代碼的小馬閱讀 479評(píng)論 0 0
  • .一.進(jìn)程 進(jìn)程:是指在系統(tǒng)中正在運(yùn)行的一個(gè)應(yīng)用程序,每個(gè)進(jìn)程之間是獨(dú)立的,每個(gè)進(jìn)程均運(yùn)行在其專用且受保護(hù)的內(nèi)存空...
    IIronMan閱讀 4,604評(píng)論 1 33
  • 我是個(gè)感性的人!即使失去了十年的愛(ài)情;經(jīng)歷了離婚;我依然相信愛(ài)情;相信自己會(huì)有幸福! 老天是眷顧我的!讓我在浮華的...
    靜宸宸閱讀 439評(píng)論 1 2
  • 雖然很難過(guò),但你我終究是匆匆流年里的過(guò)客。
    帥帥的小新閱讀 175評(píng)論 0 0
  • 今天是2017.2.28,離西域男孩解散已經(jīng)有六年多的時(shí)間了,那個(gè)曾囊括無(wú)數(shù)大獎(jiǎng),打破無(wú)數(shù)記錄的愛(ài)爾蘭樂(lè)隊(duì),那群讓...
    喬笛閱讀 2,164評(píng)論 56 36

友情鏈接更多精彩內(nèi)容