iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作

本文介绍了一个iOS应用中的多线程示例,演示了如何在子线程执行耗时任务的同时,更新主线程中的进度条显示。通过使用NSThread和定时器实现了良好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处,原文网址:http://blog.youkuaiyun.com/m_changgong/article/details/8213964  作者:张燕广

实现的功能:1)演示多线程开发。2)子线程中模拟耗时操作,然后通知主线程更新进度条。

关键词:多线程 NSThread 定时器

1、新建视图控制器ViewController.m(不带xib),作为根视图控制器,通过ViewController的-(void)loadView方法构建UI,ViewController.h如下:

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController{  
  4.     CGFloat progressValue; //进度条的值  
  5. }  
  6.   
  7. @property(strong,nonatomic)UIProgressView *progress;   
  8. @property(strong,nonatomic)UILabel *showValue;  
  9. @property(strong,nonatomic)UIButton *startThread;  
  10.   
  11. @end  

ViewController.m如下:

[cpp]  view plain copy
  1. #import "ViewController.h"  
  2.   
  3. @implementation ViewController  
  4. @synthesize progress;  
  5. @synthesize showValue;  
  6. @synthesize startThread;  
  7.   
  8. -(void)loadView{  
  9.     CGRect appFrame = [UIScreen mainScreen].applicationFrame;  
  10.     UIView *view = [[UIView alloc]initWithFrame:appFrame];  
  11.     self.view = view;  
  12.       
  13.     //设置背景颜色  
  14.     UIColor *bgcolor = [UIColor grayColor];  
  15.     [self.view setBackgroundColor:bgcolor];  
  16.       
  17.     [self initViews];  
  18. }  
  19.   
  20. //初始化视图组件  
  21. -(void)initViews{  
  22.     //初始化progress  
  23.     CGRect frame = CGRectMake(50, 50, 200, 30);  
  24.     progress = [[UIProgressView alloc]initWithFrame:frame];  
  25.     [self.view addSubview:progress];  
  26.       
  27.     //初始化showValue  
  28.     showValue = [[UILabel alloc]init];  
  29.     frame = showValue.frame;  
  30.     frame.origin.x = CGRectGetMaxX(progress.frame)+10;  
  31.     frame.origin.y = CGRectGetMinY(progress.frame);  
  32.     frame.size.width = 35;  
  33.     frame.size.height = 15;  
  34.     showValue.frame = frame;  
  35.     showValue.backgroundColor = [UIColor redColor];  
  36.     showValue.text = @"0.0";  
  37.     [self.view addSubview:showValue];  
  38.       
  39.     //初始化startThread  
  40.     startThread = [[UIButton alloc]init];  
  41.     frame = startThread.frame;  
  42.     frame.origin.x = 110;  
  43.     frame.origin.y = 80;  
  44.     frame.size.width = 100;  
  45.     frame.size.height = 50;  
  46.     startThread.frame = frame;  
  47.     UIImage *img = [UIImage imageNamed:@"btn.png"];  
  48.     [startThread setBackgroundImage:img forState:UIControlStateNormal];  
  49.     [startThread setTitleColor:[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];  
  50.     [startThread setTitle:@"开启子线程" forState:UIControlStateNormal];  
  51.     //设置事件  
  52.     [startThread addTarget:self action:@selector(startThreadButtonPressed) forControlEvents:UIControlEventTouchUpInside];  
  53.     [self.view addSubview:startThread];  
  54. }  
  55.   
  56. //开启子线程  
  57. -(void)startThreadButtonPressed{  
  58.     progress.progress = 0.0;  
  59.     showValue.text = @"0.0";  
  60.     startThread.hidden = YES;  
  61.     //该语句会让主线程堵塞2秒,这就是为什么要将耗时操作放在子线程的原因之一  
  62.     //[NSThread sleepForTimeInterval:2];  
  63.       
  64.     //开启一个新线程  
  65.     [NSThread detachNewThreadSelector:@selector(doJobInBackground) toTarget:self withObject:nil];  
  66. }  
  67.   
  68. //子线程,后台执行工作  
  69. -(void)doJobInBackground{  
  70.     //睡眠,模拟子线程中耗时操作  
  71.     [NSThread sleepForTimeInterval:2];  
  72.     //通知主线程执行更新操作  
  73.     [self performSelectorOnMainThread:@selector(invalidateProgress) withObject:nil waitUntilDone:NO];  
  74. }  
  75.   
  76. //更新进度  
  77. -(void)invalidateProgress{  
  78.     progressValue = [progress progress];  
  79.     showValue.text = [NSString stringWithFormat:@"%.2f",progressValue];  
  80.     if(progressValue < 1.0){  
  81.         progress.progress = progressValue+0.02;  
  82.         //启动定时器  
  83.         [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(invalidateProgress) userInfo:nil repeats:NO];  
  84.     }else{  
  85.         progress.progress = 1.0;  
  86.         showValue.text = @"1.0";  
  87.         startThread.hidden = NO;  
  88.     }  
  89. }  
  90.   
  91. - (void)viewDidUnload  
  92. {  
  93.     [super viewDidUnload];  
  94.     // Release any retained subviews of the main view.  
  95.     progress = nil;  
  96.     showValue = nil;  
  97.     startThread = nil;  
  98. }  
  99.   
  100. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  101. {  
  102.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  103. }  
  104.   
  105. @end  

2、运行效果如下:

内容概要:本文从关键概念、核心技巧、应用场景、代码案例分析及未来发展趋势五个维度探讨了Python编程语言的进阶之路。关键概念涵盖装饰器、生成器、上下文管理器、元类和异步编程,这些概念有助于开发者突破基础认知的核心壁垒。核心技巧方面,介绍了内存优化、性能加速、代码复用和异步处理的方法,例如使用生成器处理大数据流、numba库加速计算密集型任务等。应用场景展示了Python在大数据处理、Web开发、人工智能和自动化运维等多个领域的广泛运用,特别是在FastAPI框架中构建异步API服务的实战案例,详细分析了装饰器日志记录、异步数据库查询和性能优化技巧。最后展望了Python的未来发展趋势,包括异步编程的普及、类型提示的强化、AI框架的深度整合以及多语言协同。 适合人群:已经掌握Python基础语法,希望进一步提升编程技能的开发者,特别是有意向从事数据科学、Web开发或AI相关工作的技术人员。 使用场景及目标:①掌握Python进阶概念和技术,如装饰器、生成器、异步编程等,提升代码质量和效率;②学习如何在实际项目中应用这些技术,如通过FastAPI构建高效的异步API服务;③了解Python在未来编程领域的潜在发展方向,为职业规划提供参考。 阅读建议:本文不仅提供了理论知识,还包含了丰富的实战案例,建议读者在学习过程中结合实际项目进行练习,特别是尝试构建自己的异步API服务,并通过调试代码加深理解。同时关注Python社区的发展动态,及时掌握最新的技术和工具。
内容概要:本文档《Rust系统编程实战》详细介绍了Rust在系统编程领域的应用,强调了其内存安全、零成本抽象和高性能的特点。文档分为三个主要部分:核心实战方向、典型项目案例和技术关键点。在核心实战方向中,重点讲解了unsafe编程、FFI(外部函数接口)和底层API调用,涉及操作系统组件开发、网络编程、设备驱动开发、系统工具开发和嵌入式开发等多个领域,并列出了每个方向所需的技术栈和前置知识。典型项目案例部分以Linux字符设备驱动为例,详细描述了从环境搭建到核心代码实现的具体步骤,包括使用bindgen生成Linux内核API的Rust绑定,定义设备结构体,以及实现驱动核心函数。 适合人群:对系统编程有兴趣并有一定编程基础的开发者,尤其是那些希望深入了解操作系统底层机制、网络协议栈或嵌入式系统的工程师。 使用场景及目标:①掌握Rust在不同系统编程场景下的应用,如操作系统组件开发、网络编程、设备驱动开发等;②通过实际项目(如Linux字符设备驱动)的学习,理解Rust与操作系统内核的交互逻辑;③提高对unsafe编程、FFI和底层API调用的理解和运用能力。 阅读建议:由于文档内容较为深入且涉及多个复杂概念,建议读者在学习过程中结合实际操作进行练习,特别是在尝试实现Linux字符设备驱动时,务必按照文档提供的步骤逐步进行,并多加调试和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值