当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS 创建并发线程的实例详解

IOS 创建并发线程的实例详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

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 并发线程的实例,如有疑问大家可以留言讨论,共同进步,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网