IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解
IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解
創(chuàng)建并發(fā)線程
主線程一般都是處理UI界面及用戶交互的事兒的。其他的事一般就要另外的線程去處理,如下載,計(jì)算等。。。
現(xiàn)在先簡單創(chuàng)建3個(gè)線程,分別打印出1-1000,,為了方便,線程3就放在主線程中執(zhí)行。
- (void) firstCounter{ @autoreleasepool { NSUInteger counter = 0; for (counter = 0; counter < 1000; counter++){ NSLog(@"First Counter = %lu", (unsigned long)counter); } } }
- (void) secondCounter{ @autoreleasepool { NSUInteger counter = 0; for (counter = 0; counter < 1000; counter++){ NSLog(@"Second Counter = %lu", (unsigned long)counter); } } }
- (void) thirdCounter{ NSUInteger counter = 0; for (counter = 0; counter < 1000; counter++){ NSLog(@"Third Counter = %lu", (unsigned long)counter); } }
- (void)viewDidLoad { [super viewDidLoad]; [NSThread detachNewThreadSelector:@selector(firstCounter) toTarget:self withObject:nil]; [NSThread detachNewThreadSelector:@selector(secondCounter) toTarget:self withObject:nil]; /* Run this on the main thread */ [self thirdCounter]; }
由于thirdCounter 函數(shù)沒有運(yùn)行在單獨(dú)的線程中,所以不需要自動(dòng)釋放池(autorelease pool)。這個(gè)方法將在應(yīng)用程序的主線程中運(yùn)行,每一個(gè)Cocoa Touch程序都會(huì)自
動(dòng)的給該主線程創(chuàng)建一個(gè)自動(dòng)釋放池。
在代碼的最后通過調(diào)用 detachNewThreadSelector,把將第一個(gè)計(jì)數(shù)器和第二個(gè)計(jì)數(shù)器運(yùn)行在獨(dú)立的線程中。現(xiàn)在,如果你運(yùn)行程序,將會(huì)在控制臺(tái)窗口看到如下信息:
Second Counter = 921 Third Counter = 301 Second Counter = 922 Second Counter = 923 Second Counter = 924 First Counter = 956 Second Counter = 925 Counter = 957 Second Counter = 926 First Counter = 958 Third Counter = 302 Second Counter = 927 Third Counter = 303 Second Counter = 928
可以看出,這三個(gè)計(jì)時(shí)器是同時(shí)運(yùn)行的,他們輸出的內(nèi)容是隨機(jī)交替的。 每一個(gè)線程必須創(chuàng)建一個(gè) autorelease pool。在 autorelease pool 被 release 之前,autorelease pool 會(huì)一直持有被 autoreleased 的對(duì)象的引用。在引用計(jì)數(shù)內(nèi)存管理環(huán)境中這是一個(gè)非常重要的機(jī)制,例如Cocoa Touch中的對(duì)象就能夠被autoreleased。無論何時(shí),在創(chuàng)建一個(gè)對(duì)象實(shí)例時(shí),該對(duì)象的引用計(jì)數(shù)是1,但是當(dāng)創(chuàng)建的autorelease pool對(duì)象被release了,那么 autorelease 的對(duì)象同樣會(huì)發(fā)送一個(gè) release 消息,如果此時(shí),它的引用計(jì)數(shù)仍然是 1,那么該對(duì)象將被銷毀。
每一個(gè)線程都需要?jiǎng)?chuàng)建一個(gè) autorelease pool,當(dāng)做是該線程第一個(gè)被創(chuàng)建的對(duì)象。如果不這樣做,如果不這樣做,當(dāng)線程退出的時(shí)候,你分配在線程中的對(duì)象會(huì)發(fā)生內(nèi)存泄露。為了更好的理解,我們來看看下面的代碼:
- (void) autoreleaseThread:(id)paramSender{ NSBundle *mainBundle = [NSBundle mainBundle]; NSString *filePath = [mainBundle pathForResource:@"AnImage" ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:filePath]; /* Do something with the image */ NSLog(@"Image = %@", image); } - (void)viewDidLoad { [super viewDidLoad]; [NSThread detachNewThreadSelector:@selector(autoreleaseThread:) toTarget:self withObject:self]; } 如果你運(yùn)行這段代碼,,你就會(huì)在控制臺(tái)窗口看到這樣的輸出信息: *** __NSAutoreleaseNoPool(): Object 0x5b2c990 of class NSCFString autoreleased with no pool in place - just leaking *** __NSAutoreleaseNoPool(): Object 0x5b2ca30 of class NSPathStore2 autoreleased with no pool in place - just leaking *** __NSAutoreleaseNoPool(): Object 0x5b205c0 of class NSPathStore2 autoreleased with no pool in place - just leaking *** __NSAutoreleaseNoPool(): Object 0x5b2d650 of class UIImage autoreleased with no pool in place - just leaking
上面的信息顯示了我們創(chuàng)建的 autorelease 的 UIImage 實(shí)例產(chǎn)生了一個(gè)內(nèi)存泄露,另外,F(xiàn)ilePath 和其他的對(duì)象也產(chǎn)生了泄露。這是因?yàn)樵谖覀兊木€程中,沒有在開始的時(shí)候創(chuàng)建和初始化一個(gè)autorelease pool。下面是正確的代碼,你可以測(cè)試一下,確保它沒有內(nèi)存泄露:
- (void) autoreleaseThread:(id)paramSender{ @autoreleasepool { NSBundle *mainBundle = [NSBundle mainBundle]; NSString *filePath = [mainBundle pathForResource:@"AnImage" ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:filePath]; /* Do something with the image */ NSLog(@"Image = %@", image); } }
以上使用關(guān)于IOS 并發(fā)線程的實(shí)例,如有疑問大家可以留言討論,共同進(jìn)步,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
理解iOS多線程應(yīng)用的開發(fā)以及線程的創(chuàng)建方法
這篇文章主要介紹了理解iOS多線程應(yīng)用的開發(fā)以及線程的創(chuàng)建方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11改變iOS應(yīng)用中UITableView的背景顏色與背景圖片的方法
這篇文章主要介紹了改變iOS應(yīng)用中UITableView的背景顏色與背景圖片的方法,將UITableView的header、footer設(shè)成clearColor時(shí)要注意實(shí)際效果是否真的變透明,需要的朋友可以參考下2016-03-03以代碼實(shí)例總結(jié)iOS應(yīng)用開發(fā)中數(shù)據(jù)的存儲(chǔ)方式
這篇文章主要介紹了iOS應(yīng)用開發(fā)中數(shù)據(jù)的存儲(chǔ)方式的實(shí)例總結(jié),代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-02-02IOS實(shí)現(xiàn)輸入驗(yàn)證碼、密碼按位分割(二)
這篇文章主要介紹了IOS實(shí)現(xiàn)輸入驗(yàn)證碼、密碼按位分割的方法,在App內(nèi),密碼及驗(yàn)證碼的輸入,采用按位輸入的方法,且位與位之間有分隔線,感興趣的小伙伴們可以參考一下2016-01-01iOS10適配之權(quán)限Crash問題的完美解決方案
這篇文章主要為大家詳細(xì)介紹了iOS10適配之權(quán)限Crash問題的完美解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09iOS tableview實(shí)現(xiàn)頂部拉伸效果
這篇文章主要為大家詳細(xì)介紹了iOS tableview實(shí)現(xiàn)頂部拉伸效果,以及頭部拉伸效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05