iOS多线程 iOS开发Demo(示例程序)源代码
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版)
- iOS程序源代码下载链接:
01.大任务.zip
225.8 KB - //
- // ViewController.m
- // 01.大任务
- //
- // Created by apple on 13-12-27.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"ViewController.h"
- @interfaceViewController()
- {
- //全局的操作队列,由它统一控制所有的NSOperation的操作调度
- NSOperationQueue *_queue;
- }
- @property(weak,nonatomic)IBOutletUIImageView*imageView;
- @end
- @implementationViewController
- /**
- 无论使用哪种多线程技术都可以使用
- [NSThread currentThread]跟踪查看当前执行所在的线程情况。
- num = 1表示在主线程上执行的任务
- ================================================================
- 1. NSObject多线程技术
- 1>使用performSelectorInBackground可以开启后台线程,执行selector选择器选择的方法
- 2>使用performSelectorOnMainThread可以重新回到主线程执行任务,通常用于后台线程更新界面UI时使用
- 3> [NSThread sleepForTimeInterval:1.0f];
- 让当前线程休眠,通常在程序开发中,用于模拟耗时操作,以便跟踪不同的并发执行情况!
- 但是:在程序发布时,千万不要保留此方法!不要把测试中的代码交给客户,否则会造成不好的用户体验。
- 提示:使用performSelectorInBackground也可以直接修改UI,但是强烈不建议使用。
- 注意:在使用NSThread或者NSObject的线程方法时,一定要使用自动释放池,否则容易出现内存泄露。
- ================================================================
- 2. NSThread的多线程技术
- 1>类方法直接开启后台线程,并执行选择器方法
- detachNewThreadSelector
- 2>成员方法,在实例化线程对象之后,需要使用start执行选择器方法
- initWithTarget
- 对于NSThread的简单使用,可以用NSObject的performSelectorInBackground替代
- 同时,在NSThread调用的方法中,同样要使用autoreleasepool进行内存管理,否则容易出现内存泄露。
- //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
- ================================================================
- 3. NSOperation,面向对象的多线程技术
- 1>使用步骤:
- 1)实例化操作
- a) NSInvocationOperation
- b) NSBlockOperation
- 2)将操作添加到队列NSOperationQueue即可启动多线程执行
- 2>更新UI使用主线程队列
- [NSOpeationQueue mainQueue] addOperation ^{};
- 3>操作队列的setMaxConcurrentOperationCount
- 可以设置同时并发的线程数量!
- 提示:此功能仅有NSOperation有!
- 4>使用addDependency可以设置任务的执行先后顺序,同时可以跨操作队列指定依赖关系
- 提示:在指定依赖关系时,注意不要循环依赖,否则不工作。
- //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
- ================================================================
- 4. GCD,C语言
- */
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- NSLog(@"%@", [NSThreadcurrentThread]);
- //实例化操作队列
- _queue= [[NSOperationQueuealloc]init];
- }
- #pragma mark -操作
- //耗时操作演示
- - (void)bigDemo
- {
- //自动释放池
- //负责其他线程上的内存管理,在使用NSThread或者NSObject的线程方法时,一定要使用自动释放池
- //否则容易出现内存泄露。
- @autoreleasepool{
- // //模拟网络下载延时
- // for (NSInteger i = 0; i < 1000; i++) {
- // NSString *str = [NSString stringWithFormat:@"%d", i];
- //
- // //提示:NSLog是非常耗时的操作!
- // NSLog(@"大任务-> %@", str);
- // }
- NSLog(@"%@", [NSThreadcurrentThread]);
- //模拟网络下载延时,睡眠1秒,通常是在开发中测试使用。
- [NSThreadsleepForTimeInterval:1.0f];
- //强烈不建议直接在后台线程更新界面UI!
- //模拟获取到下载的图像
- UIImage*image = [UIImageimageNamed:@"头像1"];
- //在主线程更新图像
- //使用self调用updateImage方法在主线程更新图像
- // [self performSelectorOnMainThread:@selector(updateImage:) withObject:image waitUntilDone:YES];
- //使用imageView的setImage方法在主线程更新图像
- [_imageViewperformSelectorOnMainThread:@selector(setImage:)withObject:imagewaitUntilDone:YES];
- // ....
- }
- }
- #pragma mark -更新图像,模拟从网络上下载完图片后,更新界面的操作
- - (void)updateImage:(UIImage*)image
- {
- NSLog(@"更新图像-> %@", [NSThreadcurrentThread]);
- _imageView.image= image;
- }
- #pragma mark - Actions
- - (IBAction)bigTask
- {
- //本方法中的所有代码都是在主线程中执行的
- // NSObject多线程技术
- NSLog(@"执行前->%@", [NSThreadcurrentThread]);
- // performSelectorInBackground是将bigDemo的任务放在后台线程中执行
- [selfperformSelectorInBackground:@selector(bigDemo)withObject:nil];
- NSLog(@"执行后->%@", [NSThreadcurrentThread]);
- // [self bigDemo];
- NSLog(@"执行完毕");
- }
- - (IBAction)smallTask
- {
- NSString*str =nil;
- for(NSIntegeri =0; i <50000; i++) {
- str = [NSStringstringWithFormat:@"%d", i];
- }
- NSLog(@"小任务-> %@", str);
- }
- #pragma mark NSThread演练
- - (IBAction)threadDemo
- {
- //新建一个线程,调用@selector方法
- // [NSThread detachNewThreadSelector:@selector(bigDemo) toTarget:self withObject:nil];
- //成员方法
- NSThread*thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(bigDemo)object:nil];
- //启动start线程
- [threadstart];
- }
- #pragma mark - NSOperation演练
- - (void)opAction
- {
- NSLog(@"%@", [NSThreadcurrentThread]);
- //模拟延时
- [NSThreadsleepForTimeInterval:1.0f];
- //模拟获取到图像
- UIImage*image = [UIImageimageNamed:@"头像1"];
- //设置图像,在主线程队列中设置
- [[NSOperationQueuemainQueue]addOperationWithBlock:^{
- _imageView.image= image;
- }];
- }
- #pragma mark invocation
- - (IBAction)operationDemo1
- {
- NSInvocationOperation*op1 = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(opAction)object:nil];
- //如果使用start,会在当前线程启动操作
- // [op1 start];
- // 1.一旦将操作添加到操作队列,操作就会启动
- [_queueaddOperation:op1];
- }
- #pragma mark blockOperation
- - (IBAction)operationDemo2
- {
- //用block的最大好处,可以将一组相关的操作,顺序写在一起,便于调试以及代码编写
- [_queueaddOperationWithBlock:^{
- NSLog(@"%@", [NSThreadcurrentThread]);
- //模拟延时
- [NSThreadsleepForTimeInterval:1.0f];
- //模拟获取到图像
- UIImage*image = [UIImageimageNamed:@"头像1"];
- //设置图像,在主线程队列中设置
- [[NSOperationQueuemainQueue]addOperationWithBlock:^{
- _imageView.image= image;
- }];
- }];
- }
- #pragma mark模仿下载网络图像
- - (IBAction)operationDemo3:(id)sender
- {
- // 1.下载
- NSBlockOperation*op1 = [NSBlockOperationblockOperationWithBlock:^{
- NSLog(@"下载%@", [NSThreadcurrentThread]);
- }];
- // 2.滤镜
- NSBlockOperation*op2 = [NSBlockOperationblockOperationWithBlock:^{
- NSLog(@"滤镜%@", [NSThreadcurrentThread]);
- }];
- // 3.显示
- NSBlockOperation*op3 = [NSBlockOperationblockOperationWithBlock:^{
- NSLog(@"更新UI %@", [NSThreadcurrentThread]);
- }];
- //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
- //添加操作之间的依赖关系,所谓“依赖”关系,就是等待前一个任务完成后,后一个任务才能启动
- //依赖关系可以跨线程队列实现
- //提示:在指定依赖关系时,注意不要循环依赖,否则不工作。
- [op2addDependency:op1];
- [op3addDependency:op2];
- // [op1 addDependency:op3];
- [_queueaddOperation:op1];
- [_queueaddOperation:op2];
- [[NSOperationQueuemainQueue]addOperation:op3];
- }
- #pragma mark限制线程数量
- - (IBAction)operationDemo4
- {
- //控制同时最大并发的线程数量
- [_queuesetMaxConcurrentOperationCount:2];
- for(NSIntegeri =0; i <200; i++) {
- NSBlockOperation*op = [NSBlockOperationblockOperationWithBlock:^{
- NSLog(@"%@", [NSThreadcurrentThread]);
- }];
- [_queueaddOperation:op];
- }
- }
- #pragma mark - GCD演练
- - (IBAction)gcdDemo1
- {
- /**
- GCD就是为了在“多核”上使用多线程技术
- 1>要使用GCD,所有的方法都是dispatch开头的
- 2>名词解释
- global 全局
- queue 队列
- async 异步
- sync 同步
- 3>要执行异步的任务,就在全局队列中执行即可
- dispatch_async异步执行控制不住先后顺序
- 4>关于GCD的队列
- 全局队列 dispatch_get_global_queue
- 参数:优先级DISPATCH_QUEUE_PRIORITY_DEFAULT
- 始终是0
- 串行队列
- 主队列
- 5>异步和同步于方法名无关,与运行所在的队列有关!
- 提示:要熟悉队列于同步、异步的运行节奏,一定需要自己编写代码测试!
- 同步主要用来控制方法的被调用的顺序
- */
- // 1.队列
- dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
- // 2.将任务异步(并发)执行
- dispatch_async(queue, ^{
- NSLog(@"a->%@", [NSThreadcurrentThread]);
- });
- dispatch_async(queue, ^{
- NSLog(@"b->%@", [NSThreadcurrentThread]);
- });
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"main - > %@", [NSThreadcurrentThread]);
- });
- }
- @end
连续点击两次大任务按钮,运行结果 |
执行前-><NSThread: 0x764bcb0>{name = (null), num = 1}
执行后-><NSThread: 0x764bcb0>{name = (null), num = 1}
执行完毕
bigDemo<NSThread: 0x768d8f0>{name = (null), num = 3}
执行前-><NSThread: 0x764bcb0>{name = (null), num = 1}
bigDemo<NSThread: 0x755c8d0>{name = (null), num = 4}
执行后-><NSThread: 0x764bcb0>{name = (null), num = 1}
执行完毕
结论 |
performSelectorInBackground:withObject:该方法会使方法在后台运行,并负责为其分配线程号,一个线程3结束后,不会被回收.再次调用performSelectorInBackground:withObject:会分配4而非3
连续点击NSThread,运行结果 |
2013-12-27 19:06:01.311 1226-01-大任务[4742:51b] bigDemo<NSThread: 0x753d5d0>{name = (null), num = 3}
2013-12-27 19:06:06.266 1226-01-大任务[4742:440b] bigDemo<NSThread: 0x753f1f0>{name = (null), num = 4}
2013-12-27 19:06:07.060 1226-01-大任务[4742:4503] bigDemo<NSThread: 0x7540cb0>{name = (null), num = 5}
2013-12-27 19:06:07.659 1226-01-大任务[4742:440f] bigDemo<NSThread: 0x753d9f0>{name = (null), num = 6}
2013-12-27 19:06:08.318 1226-01-大任务[4742:4507] bigDemo<NSThread: 0x7540cb0>{name = (null), num = 7}
结论 |
连续点击NSOperation按钮,运行结果 |
2013-12-27 19:24:16.153 1226-01-大任务[4858:1703] <NSThread: 0x7686610>{name = (null), num = 3}
2013-12-27 19:24:18.755 1226-01-大任务[4858:4203] <NSThread: 0x768acf0>{name = (null), num = 4}
2013-12-27 19:24:19.245 1226-01-大任务[4858:1103] <NSThread: 0x768aa40>{name = (null), num = 5}
2013-12-27 19:24:19.724 1226-01-大任务[4858:1703] <NSThread: 0x7686610>{name = (null), num = 3}
2013-12-27 19:24:20.205 1226-01-大任务[4858:4703] <NSThread: 0x768b7e0>{name = (null), num = 6}
结论 |
连续点击NSBlockOperation按钮,运行结果 |
2013-12-27 19:24:16.153 1226-01-大任务[4858:1703] <NSThread: 0x7686610>{name = (null), num = 3}
2013-12-27 19:24:18.755 1226-01-大任务[4858:4203] <NSThread: 0x768acf0>{name = (null), num = 4}
2013-12-27 19:24:19.245 1226-01-大任务[4858:1103] <NSThread: 0x768aa40>{name = (null), num = 5}
2013-12-27 19:24:19.724 1226-01-大任务[4858:1703] <NSThread: 0x7686610>{name = (null), num = 3}
2013-12-27 19:24:20.205 1226-01-大任务[4858:4703] <NSThread: 0x768b7e0>{name = (null), num = 6}
结论 |
//用block的最大好处,可以将一组相关的操作,顺序写在一起,便于调试以及代码编写
限制并发线程数,运行结果 |
黄色部分是时间,结果发现:相同的时间只可能出现两次.
2013-12-27 20:00:58.088 1226-01-大任务[5127:1103] <NSThread: 0x7667740>{name = (null), num = 3}
2013-12-27 20:00:58.092 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.093 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.096 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.096 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.099 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.100 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.101 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.102 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.105 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.106 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.107 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
2013-12-27 20:00:58.107 1226-01-大任务[5127:4107] <NSThread: 0x7530f50>{name = (null), num = 6}
2013-12-27 20:00:58.125 1226-01-大任务[5127:1703] <NSThread: 0x754ad60>{name = (null), num = 4}
2013-12-27 20:00:58.126 1226-01-大任务[5127:4107] <NSThread: 0x7530f50>{name = (null), num = 6}
2013-12-27 20:00:58.129 1226-01-大任务[5127:1103] <NSThread: 0x7667740>{name = (null), num = 3}
2013-12-27 20:00:58.129 1226-01-大任务[5127:1703] <NSThread: 0x754ad60>{name = (null), num = 4}
2013-12-27 20:00:58.132 1226-01-大任务[5127:517] <NSThread: 0x752def0>{name = (null), num = 5}
结论 |
运行两次程序,并在每次运行时连续点击gcd按钮三次,运行结果 |
——————————————————— <第⓵次运行gcd>———————————————————————
2013-12-27 20:23:49.988 1226-01-大任务[5270:1103] ###########################-><NSThread: 0x75848f0>{name = (null), num = 3}
2013-12-27 20:23:49.987 1226-01-大任务[5270:1903] ###################-><NSThread: 0x76bfb20>{name = (null), num = 4}
2013-12-27 20:23:54.337 1226-01-大任务[5270:1103] ###########################-><NSThread: 0x75848f0>{name = (null), num = 3}
2013-12-27 20:23:54.337 1226-01-大任务[5270:1103] ###################-><NSThread: 0x75848f0>{name = (null), num = 3}
2013-12-27 20:23:59.436 1226-01-大任务[5270:1103] ###########################-><NSThread: 0x75848f0>{name = (null), num = 3}
2013-12-27 20:23:59.437 1226-01-大任务[5270:1103] ###################-><NSThread: 0x75848f0>{name = (null), num = 3}
——————————————————— <第⓶次运行gcd>———————————————————————
2013-12-27 20:24:49.950 1226-01-大任务[5288:1a03] ###################-><NSThread: 0x76911d0>{name = (null), num = 4}
2013-12-27 20:24:49.949 1226-01-大任务[5288:1103] ###########################-><NSThread: 0x7687e20>{name = (null), num = 3}
2013-12-27 20:24:53.798 1226-01-大任务[5288:1a03] ###################-><NSThread: 0x76911d0>{name = (null), num = 4}
2013-12-27 20:24:53.798 1226-01-大任务[5288:1103] ###########################-><NSThread: 0x7687e20>{name = (null), num = 3}
2013-12-27 20:24:55.321 1226-01-大任务[5288:1a03] ###################-><NSThread: 0x76911d0>{name = (null), num = 4}
2013-12-27 20:24:55.321 1226-01-大任务[5288:1103] ###########################-><NSThread: 0x7687e20>{name = (null), num = 3}
结论 |
- 要执行异步的任务,就在全局队列dispatch_get_global_queue中执行即可
- dispatch_async异步执行控制不住先后顺序
//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3494810.html
作者:
出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
本文版权归作者和博客园共有,欢迎转载,
但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。