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 的對象的引用。在引用計(jì)數(shù)內(nèi)存管理環(huán)境中這是一個(gè)非常重要的機(jī)制,例如Cocoa Touch中的對象就能夠被autoreleased。無論何時(shí),在創(chuàng)建一個(gè)對象實(shí)例時(shí),該對象的引用計(jì)數(shù)是1,但是當(dāng)創(chuàng)建的autorelease pool對象被release了,那么 autorelease 的對象同樣會(huì)發(fā)送一個(gè) release 消息,如果此時(shí),它的引用計(jì)數(shù)仍然是 1,那么該對象將被銷毀。
每一個(gè)線程都需要?jiǎng)?chuàng)建一個(gè) autorelease pool,當(dāng)做是該線程第一個(gè)被創(chuàng)建的對象。如果不這樣做,如果不這樣做,當(dāng)線程退出的時(shí)候,你分配在線程中的對象會(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 和其他的對象也產(chǎn)生了泄露。這是因?yàn)樵谖覀兊木€程中,沒有在開始的時(shí)候創(chuàng)建和初始化一個(gè)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ā)線程的實(shí)例,如有疑問大家可以留言討論,共同進(jìn)步,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
iOS開發(fā)之UIMenuController使用示例詳解
這篇文章主要為大家介紹了iOS開發(fā)之UIMenuController使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
ios實(shí)現(xiàn)搜索關(guān)鍵字高亮效果
這篇文章主要介紹了ios實(shí)現(xiàn)搜索關(guān)鍵字高亮效果的方法以及實(shí)例代碼分享,有需要的朋友參考學(xué)習(xí)下。2018-02-02
iOS實(shí)現(xiàn)類似微信和支付寶的密碼輸入框(UIKeyInput協(xié)議)
這篇文章主要介紹了iOS實(shí)現(xiàn)類似微信和支付寶的密碼輸入框,通過UIKeyInput協(xié)議為響應(yīng)者提供簡單的鍵盤輸入的功,再通過CoreGraphics繪制出密碼輸入框,感興趣的小伙伴們可以參考一下2016-08-08

