iOS多线程编程技术NSThread、Cocoa NSOperation、GCD三者使用详解

简介
iOS有三种多线程编程的技术,分别是:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. (一)NSThread  
  2.   
  3. (二)Cocoa NSOperation  
  4.   
  5. (三)GCD(全称:Grand Central Dispatch)  

三种方式的优缺点介绍:
1)NSThread

优点: NSThread 比其他两个轻量级
缺点: 需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销

2)Cocoa  NSOperation
优点: 不需要关心线程管理, 数据同步的事情,可以把精力放在自己需要执行的操作上。
Cocoa operation相关的类是NSOperation, NSOperationQueue.
NSOperation是个抽象类,使用它必须用它的子类,可以实现它或者使用它定义好的两个子类: NSInvocationOperation和NSBlockOperation.
创建NSOperation子类的对象,把对象添加到NSOperationQueue队列里执行。

3) GCD (全优点)

Grand Central dispatch(GCD)是Apple开发的一个多核编程的解决方案。在iOS4.0开始之后才能使用。GCD是一个替代NSThread, NSOperationQueue,NSInvocationOperation等技术的很高效强大的技术。


下面我用简单易于理解的代码实例来介绍NSThread在开发实践中的使用,具体使用时可以根据实际情况进行扩展:
一、NSThread的使用(基本已过时)

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. #define kURL @"http://www.iyi8.com/uploadfile/2014/0506/20140506085929652.jpg"  
  4.   
  5. @interface ViewController : UIViewController  
  6.   
  7. @end  

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4. {  
  5.     UIImage         *_image;  
  6.     UIImageView     *_imageView;  
  7.       
  8.     int             _tickets;  
  9.     int             _count;  
  10.       
  11.     NSThread        *_threadOne;  
  12.     NSThread        *_threadTwo;  
  13.     NSThread        *_threadThree;  
  14.       
  15.     NSCondition     *_condition;  
  16.       
  17.     NSLock          *_lock;  
  18. }  
  19. @end  
  20.   
  21. @implementation ViewController  
  22.   
  23. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  24. {  
  25.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  26.     if (self) {  
  27.     }  
  28.     return self;  
  29. }  

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)loadView  
  2. {  
  3.     self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];  
  4.     NSLog(@"klfaklfa ------ - - ");  
  5.       
  6.     _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)];  
  7.     [self.view addSubview:_imageView];  
  8. }  

/** 测试NSthread使用*/
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     _tickets    = 200;  
  6.     _count      = 0;  
  7.     _lock       = [[NSLock alloc] init];  
  8.       
  9.     //锁对象  
  10.     _condition = [[NSCondition alloc] init];  
  11.     //线程1  
  12.     _threadOne = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  13.     _threadOne.name = @"thread-1";  
  14.     [_threadOne start];  
  15.       
  16.     //线程2  
  17.     _threadTwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  18.     _threadTwo.name = @"thread-2";  
  19.     [_threadTwo start];  
  20.       
  21.     //线程3  
  22.     _threadThree = [[NSThread alloc] initWithTarget:self selector:@selector(run3) object:nil];  
  23.     _threadThree.name = @"thread-3";  
  24.     [_threadThree start];  
  25.       
  26.     //如果没有线程同步的lock,物品售出数量就会出现重复导致数据竞争不同步问题.加上lock之后线程同步保证了数据的正确性。  
  27.     [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];  
  28.     NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadImage:) object:kURL];  
  29.     [thread start];  
  30. }     

/** 测试NSOperationQueue使用

使用 NSOperation的方式有两种,
一种是用定义好的两个子类:NSInvocationOperation 和 NSBlockOperation。
另一种是继承NSOperation
 
如果你也熟悉Java,NSOperation就和java.lang.Runnable接口很相似。和Java的Runnable一样,NSOperation也是设计用来扩展的,只需继承重写NSOperation的一个方法main。相当与java 中Runnalbe的Run方法。然后把NSOperation子类的对象放入NSOperationQueue队列中,该队列就会启动并开始处理它。
 
NSInvocationOperation例子:
这里同样,我们实现一个下载图片的例子。新建一个Single View app,拖放一个ImageView控件到xib界面。
实现代码如下

*/

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self  
  6.     selector:@selector(downloadImage:)  
  7.      object:kURL];  
  8.        
  9.      NSOperationQueue *queue = [[NSOperationQueue alloc] init];  
  10.      [queue addOperation:operation];  
  11. }     

第二种方式继承NSOperation 
在.m文件中实现main方法,main方法编写要执行的代码即可。
 
如何控制线程池中的线程数?
队列里可以加入很多个NSOperation, 可以把NSOperationQueue看作一个线程池,可往线程池中添加操作(NSOperation)到队列中。线程池中的线程可看作消费者,从队列中取走操作,并执行它。
 
通过下面的代码设置:
 
 
  1. [queue setMaxConcurrentOperationCount:5]; 
 
线程池中的线程数,也就是并发操作数。默认情况下是-1,-1表示没有限制,这样会同时运行队列中的全部的操作。
 

/** 测试GCD的dispatch_async使用*/

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
  6.      NSURL * url = [NSURL URLWithString:kURL];  
  7.      NSData * data = [[NSData alloc]initWithContentsOfURL:url];  
  8.      UIImage *image = [[UIImage alloc]initWithData:data];  
  9.           
  10.          if (data != nil) {  
  11.              dispatch_async(dispatch_get_main_queue(), ^{  
  12.              _imageView.image = image;  
  13.             });  
  14.          }  
  15.      });  
  16. }   


(三)GCD的介绍和使用
介绍:
Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统。这建立在任务并行执行的线程池模式的基础上的。它首次发布在Mac OS X 10.6 ,iOS 4及以上也可用。
 
设计:
GCD的工作原理是:让程序平行排队的特定任务,根据可用的处理资源,安排他们在任何可用的处理器核心上执行任务。
 
一个任务可以是一个函数(function)或者是一个block。 GCD的底层依然是用线程实现,不过这样可以让程序员不用关注实现的细节。
 
GCD中的FIFO队列称为dispatch queue,它可以保证先进来的任务先得到执行。
 
dispatch queue分为下面三种:
Serial     
又称为private dispatch queues,同时只执行一个任务。Serial queue通常用于同步访问特定的资源或数据。当你创建多个Serial queue时,虽然它们各自是同步执行的,但Serial queue与Serial queue之间是并发执行的。
 
Concurrent
又称为global dispatch queue,可以并发地执行多个任务,但是执行完成的顺序是随机的。
 
Main dispatch queue
它是全局可用的serial queue,它是在应用程序主线程上执行任务的。
 
我们看看dispatch queue如何使用?
 
1、常用的方法dispatch_async
为了避免界面在处理耗时的操作时卡死,比如读取网络数据,IO,数据库读写等,我们会在另外一个线程中处理这些操作,然后通知主线程更新界面。
 
用GCD实现这个流程的操作比前面介绍的NSThread  NSOperation的方法都要简单。代码框架结构如下

/** 测试GCD的dispatch_group_async使用*/

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     //此方法可以实现监听一组任务是否完成,如果完成后通知其他操作(如界面更新),此方法在下载附件时挺有用,  
  6.      //在搪行几个下载任务时,当下载完成后通过dispatch_group_notify通知主线程下载完成并更新相应界面  
  7.      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
  8.      dispatch_group_t group = dispatch_group_create();  
  9.      dispatch_group_async(group, queue, ^{  
  10.          [NSThread sleepForTimeInterval:0.09];  
  11.        
  12.      NSLog(@"group1");  
  13.      NSURL * url = [NSURL URLWithString:kURL];  
  14.      NSData * data = [[NSData alloc]initWithContentsOfURL:url];  
  15.      _image = [[UIImage alloc]initWithData:data];  
  16.        
  17.      });  
  18.      dispatch_group_async(group, queue, ^{  
  19.          [NSThread sleepForTimeInterval:0.09];  
  20.          NSLog(@"group2");  
  21.      });  
  22.      dispatch_group_async(group, queue, ^{  
  23.          [NSThread sleepForTimeInterval:0.09];  
  24.          NSLog(@"group3");  
  25.      });  
  26.        
  27.      dispatch_group_notify(group, dispatch_get_main_queue(), ^{  
  28.          NSLog(@"updateUi");  
  29.        
  30.          _imageView.image = _image;  
  31.      });  
  32. }   

/** 测试GCD的dispatch_barrier_async使用*/

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     //是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行  
  6.     dispatch_queue_t queue = dispatch_queue_create("gcd.devdiy.com", DISPATCH_QUEUE_CONCURRENT);  
  7.     dispatch_async(queue, ^{  
  8.         [NSThread sleepForTimeInterval:2];  
  9.         NSLog(@"dispatch_async1");  
  10.     });  
  11.       
  12.     dispatch_async(queue, ^{  
  13.         [NSThread sleepForTimeInterval:4];  
  14.         NSLog(@"dispatch_async2");  
  15.     });  
  16.       
  17.     dispatch_barrier_async(queue, ^{  
  18.         NSLog(@"dispatch_barrier_async");  
  19.         [NSThread sleepForTimeInterval:4];  
  20.     });  
  21.       
  22.     dispatch_async(queue, ^{  
  23.         [NSThread sleepForTimeInterval:1];  
  24.         NSLog(@"dispatch_async");  
  25.     });  
  26. }   

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)run3  
  2. {  
  3.     while (true) {  
  4.         [_condition lock];  
  5.         [NSThread sleepForTimeInterval:3];  
  6.         NSLog(@"当前物品名称:%d,售出数量:%d,线程名-=-==: %@", _tickets, _count, [[NSThread currentThread] name]);  
  7.           
  8.         [_condition signal];  
  9.         [_condition unlock];  
  10.     }  
  11. }  
  12.   
  13. - (void)run  
  14. {  
  15.     while (true) {  
  16.         //上锁  
  17.         [_lock lock];  
  18.         if (_tickets > 0) {  
  19.             [NSThread sleepForTimeInterval:0.09];  
  20.             _count = 200 - _tickets;  
  21.             NSLog(@"当前物品名称:%d,售出数量:%d,线程名: %@", _tickets, _count, [[NSThread currentThread] name]);  
  22.             _tickets--;  
  23.         }else{  
  24.             break;  
  25.         }  
  26.           
  27.         [_lock unlock];  
  28.     }  
  29. }  
  30.   
  31. /**  
  32.  * @param string url  
  33.  */  
  34. - (void)downloadImage:(NSString *) url{  
  35.     NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];  
  36.     UIImage *image = [[UIImage alloc] initWithData:data];  
  37.     if(image == nil){  
  38.         [self updateUI:image];  
  39.     }else{  
  40.         [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];  
  41.     }  
  42. }  
  43.   
  44. - (void)updateUI:(UIImage *)image  
  45. {  
  46.     _imageView.image = image;  
  47. }  
  48.   
  49. @end  

是不是代码比NSThread  NSOperation简洁很多,而且GCD会自动根据任务在多核处理器上分配资源,优化程序。
 
系统给每一个应用程序提供了三个concurrent dispatch queues。这三个并发调度队列是全局的,它们只有优先级的不同。因为是全局的,我们不需要去创建。我们只需要通过使用函数dispath_get_global_queue去得到队列。

以上代码组织的不是很好,只简单描述了一下多线程的基本概念,后续我再添加详细应用的实例,现只做为参考,在具体实际应用中还得我们自己来进行编写发挥。希望对大家有所帮助和启发。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值