三种方式的优缺点介绍:
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等技术的很高效强大的技术。 html
下面我用简单易于理解的代码实例来介绍NSThread在开发实践中的使用,具体使用时能够根据实际状况进行扩展:
1、NSThread的使用(基本已过期) java
- #import <UIKit/UIKit.h>
-
- #define kURL @"http://www.iyi8.com/uploadfile/2014/0506/20140506085929652.jpg"
-
- @interface ViewController : UIViewController
-
- @end
- #import "ViewController.h"
-
- @interface ViewController ()
- {
- UIImage *_image;
- UIImageView *_imageView;
-
- int _tickets;
- int _count;
-
- NSThread *_threadOne;
- NSThread *_threadTwo;
- NSThread *_threadThree;
-
- NSCondition *_condition;
-
- NSLock *_lock;
- }
- @end
-
- @implementation ViewController
-
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- }
- return self;
- }
- - (void)loadView
- {
- self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
- NSLog(@"klfaklfa ------ - - ");
-
- _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)];
- [self.view addSubview:_imageView];
- }
/** 测试NSthread使用*/
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- _tickets = 200;
- _count = 0;
- _lock = [[NSLock alloc] init];
-
- //锁对象
- _condition = [[NSCondition alloc] init];
- //线程1
- _threadOne = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
- _threadOne.name = @"thread-1";
- [_threadOne start];
-
- //线程2
- _threadTwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
- _threadTwo.name = @"thread-2";
- [_threadTwo start];
-
- //线程3
- _threadThree = [[NSThread alloc] initWithTarget:self selector:@selector(run3) object:nil];
- _threadThree.name = @"thread-3";
- [_threadThree start];
-
- //若是没有线程同步的lock,物品售出数量就会出现重复致使数据竞争不一样步问题.加上lock以后线程同步保证了数据的正确性。
- [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];
- NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadImage:) object:kURL];
- [thread start];
- }
/** 测试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界面。
实现代码以下
*/ 数据库
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
- selector:@selector(downloadImage:)
- object:kURL];
-
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [queue addOperation:operation];
- }
第二种方式继承NSOperation
在.m文件中实现main方法,main方法编写要执行的代码便可。
如何控制线程池中的线程数?
队列里能够加入不少个NSOperation, 能够把NSOperationQueue看做一个线程池,可往线程池中添加操做(NSOperation)到队列中。线程池中的线程可看做消费者,从队列中取走操做,并执行它。
经过下面的代码设置:
- [queue setMaxConcurrentOperationCount:5];
线程池中的线程数,也就是并发操做数。默认状况下是-1,-1表示没有限制,这样会同时运行队列中的所有的操做。
/** 测试GCD的dispatch_async使用*/ 编程
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSURL * url = [NSURL URLWithString:<span>kURL</span>];
- NSData * data = [[NSData alloc]initWithContentsOfURL:url];
- UIImage *image = [[UIImage alloc]initWithData:data];
-
- if (data != nil) {
- dispatch_async(dispatch_get_main_queue(), ^{
- _imageView.image = image;
- });
- }
- });
- }
(三)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如何使用?
一、经常使用的方法dispatch_async
为了不界面在处理耗时的操做时卡死,好比读取网络数据,IO,数据库读写等,咱们会在另一个线程中处理这些操做,而后通知主线程更新界面。
用GCD实现这个流程的操做比前面介绍的NSThread NSOperation的方法都要简单。代码框架结构以下
/** 测试GCD的dispatch_group_async使用*/ 网络
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- //此方法能够实现监听一组任务是否完成,若是完成后通知其余操做(如界面更新),此方法在下载附件时挺有用,
- //在搪行几个下载任务时,当下载完成后经过dispatch_group_notify通知主线程下载完成并更新相应界面
- dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- dispatch_group_t group = dispatch_group_create();
- dispatch_group_async(group, queue, ^{
- [NSThread sleepForTimeInterval:0.09];
-
- NSLog(@"group1");
- NSURL * url = [NSURL URLWithString:kURL];
- NSData * data = [[NSData alloc]initWithContentsOfURL:url];
- _image = [[UIImage alloc]initWithData:data];
-
- });
- dispatch_group_async(group, queue, ^{
- [NSThread sleepForTimeInterval:0.09];
- NSLog(@"group2");
- });
- dispatch_group_async(group, queue, ^{
- [NSThread sleepForTimeInterval:0.09];
- NSLog(@"group3");
- });
-
- dispatch_group_notify(group, dispatch_get_main_queue(), ^{
- NSLog(@"updateUi");
-
- _imageView.image = _image;
- });
- }
/** 测试GCD的dispatch_barrier_async使用*/ 并发
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- //是在前面的任务执行结束后它才执行,并且它后面的任务等它执行完成以后才会执行
- dispatch_queue_t queue = dispatch_queue_create("gcd.devdiy.com", DISPATCH_QUEUE_CONCURRENT);
- dispatch_async(queue, ^{
- [NSThread sleepForTimeInterval:2];
- NSLog(@"dispatch_async1");
- });
-
- dispatch_async(queue, ^{
- [NSThread sleepForTimeInterval:4];
- NSLog(@"dispatch_async2");
- });
-
- dispatch_barrier_async(queue, ^{
- NSLog(@"dispatch_barrier_async");
- [NSThread sleepForTimeInterval:4];
- });
-
- dispatch_async(queue, ^{
- [NSThread sleepForTimeInterval:1];
- NSLog(@"dispatch_async");
- });
- }
- - (void)run3
- {
- while (true) {
- [_condition lock];
- [NSThread sleepForTimeInterval:3];
- NSLog(@"当前物品名称:%d,售出数量:%d,线程名-=-==: %@", _tickets, _count, [[NSThread currentThread] name]);
-
- [_condition signal];
- [_condition unlock];
- }
- }
-
- - (void)run
- {
- while (true) {
- //上锁
- [_lock lock];
- if (_tickets > 0) {
- [NSThread sleepForTimeInterval:0.09];
- _count = 200 - _tickets;
- NSLog(@"当前物品名称:%d,售出数量:%d,线程名: %@", _tickets, _count, [[NSThread currentThread] name]);
- _tickets--;
- }else{
- break;
- }
-
- [_lock unlock];
- }
- }
-
- /**
- * @param string url
- */
- - (void)downloadImage:(NSString *) url{
- NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
- UIImage *image = [[UIImage alloc] initWithData:data];
- if(image == nil){
- [self updateUI:image];
- }else{
- [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
- }
- }
-
- - (void)updateUI:(UIImage *)image
- {
- _imageView.image = image;
- }
-
- @end
是否是代码比NSThread NSOperation简洁不少,并且GCD会自动根据任务在多核处理器上分配资源,优化程序。
系统给每个应用程序提供了三个concurrent dispatch queues。这三个并发调度队列是全局的,它们只有优先级的不一样。由于是全局的,咱们不须要去建立。咱们只须要经过使用函数dispath_get_global_queue去获得队列。