多線程筆記

一. 創(chuàng)建線程的3種方法

1.alloc init

- (void)threadDemo1

{

// 這一句只是 分配內(nèi)存和初始化

??NSThread?*thread = [[NSThread?alloc]?initWithTarget:self?selector:@selector(demo1:)object:@"Thread"];

??// 這一句才是啟動(dòng)線程

??[thread?start];

}

2.detachNewThreadSelector

- (void)threadDemo2

{

??[NSThread?detachNewThreadSelector:@selector(demo1:)?toTarget:self?withObject:@"detach"];

}

3.performSelectorInBackground

- (void)threadDemo3

{

??[self?performSelectorInBackground:@selector(demo1:)?withObject:@"background"];

}

performSelectorInBackground 是NSObject的一個(gè)分類方法,意味著所有的NSObject都可以使用該方法,在其他線程執(zhí)行方法!

特點(diǎn):沒(méi)有thread 字眼,一旦指定,就會(huì)立即在后臺(tái)線程執(zhí)行selector 方法,是隱式的多線程方法,這種方法在使用時(shí)更加靈活!

例如:

一個(gè) Person 類繼承自NSObject,有個(gè)方法 loadData很耗時(shí),在viewController里實(shí)例化一個(gè)Person * P,可以這樣調(diào)用:

[PperformSelectorInBackground:@selector(loadData)withObject:nil];

二、NSLog是專門用來(lái)調(diào)試的,性能非常不好,在商業(yè)軟件中,要盡量去掉

三、

[NSThread currentThread],當(dāng)前線程對(duì)象,可以在所有的多線程技術(shù)中使用,通常用來(lái)判斷是否在主線程

輸出的信息中,關(guān)注 number

Number == 1 說(shuō)明是主線程

Number != 1 說(shuō)明不是主線程

四、線程狀態(tài)

//?線程狀態(tài)的demo

- (void)threadStatusDemo

{

??//?實(shí)例化線程對(duì)象(新建)

??NSThread?*t = [[NSThread?alloc]?initWithTarget:self?selector:@selector(threadStatus)object:nil];

??//?就緒

??[t?start];

}

- (void)threadStatus

{

??NSLog(@"睡會(huì)兒");


??//?阻塞

??// sleep?是類方法,會(huì)直接休眠當(dāng)前線程

??[NSThread?sleepForTimeInterval:2.0];


??for?(int?i =?0; i <?10; i++)

??{

????if?(i ==?4)

????{

??????NSLog(@"再睡會(huì)兒");

??????//?線程執(zhí)行中,滿足某個(gè)條件時(shí),再次休眠

??????[NSThread?sleepUntilDate:[NSDate?dateWithTimeIntervalSinceNow:2.0]];

????}

????NSLog(@"%@, %d", [NSThread?currentThread], i);


????if?(i ==?8)

????{

??????//?終止

??????//?一旦終止,后續(xù)所有的代碼都不會(huì)被執(zhí)行,注意:在終止線程之前,應(yīng)該注意釋放之前分配的對(duì)象,如果是ARC?開(kāi)發(fā),需要注意,清理C語(yǔ)言框架創(chuàng)建的對(duì)象,否則會(huì)出現(xiàn)內(nèi)存泄漏

??????[NSThread?exit];

????}

??}


??NSLog(@"能來(lái)嗎?");

}

五、線程屬性

name 、isMainThread

//?線程屬性

- (void)threadPropertyDemo

{

??NSThread?*t = [[NSThread?alloc]?initWithTarget:self?selector:@selector(threadProperty)object:nil];

??//?屬性1. name:在大的商業(yè)項(xiàng)目中,通常希望程序崩潰的時(shí)候,能夠獲取到程序準(zhǔn)確執(zhí)行所在的線程

??t.name?=?@"Thread A";


??[t?start];

}

- (void)threadProperty

{

??for?(int?i =?0; i <?2; i++)

??{

????NSLog(@"%@, %d", [NSThread?currentThread], i);

??}


??//?屬性2.?判斷是否是主線程

??if?(![NSThread?isMainThread])

??{

?????//?模擬崩潰

????NSMutableArray?*array = [NSMutableArray?array];

????[array?addObject:nil];

??}

}

?著作權(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)容

  • 一、多線程基礎(chǔ) 基本概念 進(jìn)程進(jìn)程是指在系統(tǒng)中正在運(yùn)行的一個(gè)應(yīng)用程序每個(gè)進(jìn)程之間是獨(dú)立的,每個(gè)進(jìn)程均運(yùn)行在其專用且...
    AlanGe閱讀 651評(píng)論 0 0
  • 多線程基本概念 單核CPU,同一時(shí)間cpu只能處理1個(gè)線程,只有1個(gè)線程在執(zhí)行 。多線程同時(shí)執(zhí)行:是CPU快速的在...
    WeiHing閱讀 786評(píng)論 1 5
  • 上文我們簡(jiǎn)單的敘述了多線程,那么這篇我們就詳細(xì)的說(shuō)一下! 多線程技術(shù)方案 PThread 導(dǎo)入頭文件 #impor...
    Clark_new閱讀 434評(píng)論 5 2
  • A. 我對(duì)這句話的理解:可以給別人建議,但不要替別人做決定。引導(dǎo)應(yīng)該是靈活性的,多種選擇的。尊重他的選擇,同時(shí)給與...
    Selena_5b5g閱讀 355評(píng)論 0 0
  • 親愛(ài)的祥園同學(xué): 你好! 首先祝你生日快樂(lè),天天開(kāi)心,學(xué)習(xí)進(jìn)步,成為一個(gè)更優(yōu)秀的自己! 說(shuō)...
    荷包蛋的小屋閱讀 402評(píng)論 1 2

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