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



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值