最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS 創(chuàng)建并發(fā)線程的實例詳解

 更新時間:2017年07月15日 16:38:10   作者:Hi_Aaron  
這篇文章主要介紹了IOS 創(chuàng)建并發(fā)線程的實例詳解的相關(guān)資料,需要的朋友可以參考下

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ù)沒有運行在單獨的線程中,所以不需要自動釋放池(autorelease pool)。這個方法將在應(yīng)用程序的主線程中運行,每一個Cocoa Touch程序都會自
動的給該主線程創(chuàng)建一個自動釋放池。  

       在代碼的最后通過調(diào)用 detachNewThreadSelector,把將第一個計數(shù)器和第二個計數(shù)器運行在獨立的線程中?,F(xià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

       可以看出,這三個計時器是同時運行的,他們輸出的內(nèi)容是隨機交替的。 每一個線程必須創(chuàng)建一個 autorelease pool。在 autorelease pool 被 release 之前,autorelease pool 會一直持有被 autoreleased 的對象的引用。在引用計數(shù)內(nèi)存管理環(huán)境中這是一個非常重要的機制,例如Cocoa Touch中的對象就能夠被autoreleased。無論何時,在創(chuàng)建一個對象實例時,該對象的引用計數(shù)是1,但是當創(chuàng)建的autorelease pool對象被release了,那么 autorelease 的對象同樣會發(fā)送一個 release 消息,如果此時,它的引用計數(shù)仍然是 1,那么該對象將被銷毀。 

        每一個線程都需要創(chuàng)建一個 autorelease pool,當做是該線程第一個被創(chuà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];  
}  
如果你運行這段代碼,,你就會在控制臺窗口看到這樣的輸出信息:
*** __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ā)線程的實例,如有疑問大家可以留言討論,共同進步,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論

郁南县| 沙湾县| 麟游县| 栾川县| 英吉沙县| 商洛市| 东乌珠穆沁旗| 托克逊县| 繁峙县| 洞口县| 富平县| 密山市| 信阳市| 咸阳市| 蓬安县| 商南县| 乃东县| 阜新| 巴林右旗| 德惠市| 古蔺县| 香港| 灵川县| 简阳市| 连平县| 克拉玛依市| 会理县| 松阳县| 新邵县| 宜阳县| 扎鲁特旗| 赞皇县| 体育| 台东县| 攀枝花市| 淳安县| 湖州市| 侯马市| 浦北县| 宁远县| 若尔盖县|