IOS 創(chuàng)建并發(fā)線程的實例詳解
IOS 創(chuàng)建并發(fā)線程的實例詳解
創(chuàng)建并發(fā)線程
主線程一般都是處理UI界面及用戶交互的事兒的。其他的事一般就要另外的線程去處理,如下載,計算等。。。
現(xiàn)在先簡單創(chuàng)建3個線程,分別打印出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ú)的線程中,所以不需要自動釋放池(autorelease pool)。這個方法將在應(yīng)用程序的主線程中運(yùn)行,每一個Cocoa Touch程序都會自
動的給該主線程創(chuàng)建一個自動釋放池。
在代碼的最后通過調(diào)用 detachNewThreadSelector,把將第一個計數(shù)器和第二個計數(shù)器運(yùn)行在獨(dú)立的線程中?,F(xiàn)在,如果你運(yùn)行程序,將會在控制臺窗口看到如下信息:
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
可以看出,這三個計時器是同時運(yùn)行的,他們輸出的內(nèi)容是隨機(jī)交替的。 每一個線程必須創(chuàng)建一個 autorelease pool。在 autorelease pool 被 release 之前,autorelease pool 會一直持有被 autoreleased 的對象的引用。在引用計數(shù)內(nèi)存管理環(huán)境中這是一個非常重要的機(jī)制,例如Cocoa Touch中的對象就能夠被autoreleased。無論何時,在創(chuàng)建一個對象實例時,該對象的引用計數(shù)是1,但是當(dāng)創(chuàng)建的autorelease pool對象被release了,那么 autorelease 的對象同樣會發(fā)送一個 release 消息,如果此時,它的引用計數(shù)仍然是 1,那么該對象將被銷毀。
每一個線程都需要創(chuàng)建一個 autorelease pool,當(dāng)做是該線程第一個被創(chuàng)建的對象。如果不這樣做,如果不這樣做,當(dāng)線程退出的時候,你分配在線程中的對象會發(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)行這段代碼,,你就會在控制臺窗口看到這樣的輸出信息: *** __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 實例產(chǎn)生了一個內(nèi)存泄露,另外,F(xiàn)ilePath 和其他的對象也產(chǎn)生了泄露。這是因為在我們的線程中,沒有在開始的時候創(chuàng)建和初始化一個autorelease pool。下面是正確的代碼,你可以測試一下,確保它沒有內(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ā)線程的實例,如有疑問大家可以留言討論,共同進(jìn)步,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(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時要注意實際效果是否真的變透明,需要的朋友可以參考下2016-03-03以代碼實例總結(jié)iOS應(yīng)用開發(fā)中數(shù)據(jù)的存儲方式
這篇文章主要介紹了iOS應(yīng)用開發(fā)中數(shù)據(jù)的存儲方式的實例總結(jié),代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-02-02iOS10適配之權(quán)限Crash問題的完美解決方案
這篇文章主要為大家詳細(xì)介紹了iOS10適配之權(quán)限Crash問題的完美解決方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09