国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS 創建并發線程的實例詳解

2020-07-26 02:45:16
字體:
來源:轉載
供稿:網友

IOS 創建并發線程的實例詳解

創建并發線程

       主線程一般都是處理UI界面及用戶交互的事兒的。其他的事一般就要另外的線程去處理,如下載,計算等。。。
現在先簡單創建3個線程,分別打印出1-1000,,為了方便,線程3就放在主線程中執行。

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

       在代碼的最后通過調用 detachNewThreadSelector,把將第一個計數器和第二個計數器運行在獨立的線程中。現在,如果你運行程序,將會在控制臺窗口看到如下信息:

 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

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

        每一個線程都需要創建一個 autorelease pool,當做是該線程第一個被創建的對象。如果不這樣做,如果不這樣做,當線程退出的時候,你分配在線程中的對象會發生內存泄露。為了更好的理解,我們來看看下面的代碼: 

- (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

      上面的信息顯示了我們創建的 autorelease 的 UIImage 實例產生了一個內存泄露,另外,FilePath 和其他的對象也產生了泄露。這是因為在我們的線程中,沒有在開始的時候創建和初始化一個autorelease pool。下面是正確的代碼,你可以測試一下,確保它沒有內存泄露:

- (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);  }  }  

以上使用關于IOS 并發線程的實例,如有疑問大家可以留言討論,共同進步,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平乡县| 潼关县| 菏泽市| 电白县| 汝南县| 治多县| 东山县| 噶尔县| 靖安县| 泾阳县| 元江| 汉源县| 青田县| 石嘴山市| 酉阳| 漠河县| 赤水市| 云安县| 高尔夫| 漳浦县| 迁西县| 鱼台县| 延川县| 胶州市| 阿拉善右旗| 泌阳县| 台中县| 宜城市| 三门县| 天等县| 肇源县| 呼图壁县| 威远县| 会同县| 明水县| 和平县| 乐业县| 利川市| 青海省| 永兴县| 大化|