多线程之NSThread、NSOperation、NSInvocationOperation

来源:互联网 发布:知黛化妆品 编辑:程序博客网 时间:2024/04/29 21:49

IOS中支持多线程操作,使用NSThread和NSInvocationOperation可以完成多线程功能。多线程的功能主要是为了防止阻塞主线程的工作(主要是UI操作和显示),使一些耗时的的操作在另一个线程中完成,完成后可以通知主线程来进行UI上的更新。多线程功能在实际开发中用的很多,最典型的就是网络请求和处理操作,下面主要来讨论一下Cocoa中的NSThread和NSInvocationOperation:


一、NSThread

创建NSThread主要有两种方式:

1.使用类方法创建

[NSThread detachNewThreadSelector:@selector(doInBackgroud) toTarget:self withObject:nil];


2.使用传统方式创建

NSThread *thread = [[NSThreadalloc]initWithTarget:self selector:@selector(doInBackgroud)object:nil];

[thread start];


两种方式的区别:

1.第一种方式会立即调用并执行线程,第二种必须调用start方法后才会开始执行线程,在此之前可以对线程进行一些设置,比如线程优先级等。第二种方式与Java中线程的使用类似。

2.使用类方法(Convenient Method)创建的线程不需要进行内存清理,而使用initWithTarget方法创建的线程需要当retainCount为0时调用release方法释放内存。

//在另一个线程中运行的方法

-(void)doInBackgroud

{

    NSAutoreleasePool *releasePool = [[NSAutoreleasePoolalloc]init];

    //do someting...

    

    [releasePool release];

}

多线程中执行的方法必须自行进行内存管理,否则会出现警告信息。


运行程序可以看到打印信息:

2012-08-08 11:03:21.470 ThreadTest[518:f803] Thread is start...

2012-08-08 11:03:21.471 ThreadTest[518:1291b] Thread is running...

2012-08-08 11:03:24.471 ThreadTest[518:1291b] Thread is done…


完整代码如下

[cpp] view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [superviewDidLoad];  
  4.       
  5.     NSLog(@"Thread is start...");  
  6.     //使用类方法创建线程  
  7. //  [NSThread detachNewThreadSelector:@selector(doInBackgroud) toTarget:self withObject:nil];  
  8.       
  9.     //使用传统方式创建  
  10.     NSThread *thread = [[NSThreadalloc] initWithTarget:self selector:@selector(doInBackgroud) object:nil];  
  11.     [thread start];  
  12.     [thread release];  
  13. }  
  14.        
  15. //在另一个线程中运行的方法  
  16. -(void)doInBackgroud  
  17. {  
  18.     NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];  
  19.       
  20.     //do someting...  
  21.     NSLog(@"Thread is running...");  
  22.     [NSThread sleepForTimeInterval:3];  
  23.     NSLog(@"Thread is done...");  
  24.       
  25.     [releasePool release];  
  26. }  

二、NSOperation

NSOperation是Cocoa中的一个抽象类,用来封装单个任务和代码执行一项操作,由于是抽象类,所以不能直接实例化使用,必须定义子类继承该抽象类来实现,比较常用的NSOperation的子类有NSInvocationOperation,另外,也可以自己继承NSOperation来实现线程的操作。

另外会使用到操作队列NSOperationQueue,它相当于一个线程队列或者可以叫做线程池,可以顺序执行队列中的操作,也可以设置队列中操作的优先级。

.h头文件

[cpp] view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface MyTaskOperation : NSOperation  
  4.   
  5. @end  

.m实现文件:

[cpp] view plain copy
  1. #import "MyTaskOperation.h"  
  2.   
  3. @implementation MyTaskOperation  
  4.   
  5. //相当于Java线程中的run方法  
  6. -(void)main  
  7. {  
  8.     //do someting...  
  9.     NSLog(@"Thread is running...");  
  10.     [NSThreadsleepForTimeInterval:3];  
  11.     NSLog(@"Thread is done...");  
  12. }  
  13. @end  

使用方法如下:

[cpp] view plain copy
  1. //使用NSOperation子类来创建线程  
  2.     NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];  
  3.     MyTaskOperation *myTask = [[MyTaskOperation alloc] init];  
  4.     [operationQueue addOperation:myTask];  
  5.     [myTask release];  
  6.     [operationQueue release];  

运行结果和上面结果一样。


三、NSInvocationOperation

NSOperation的子类NSInvocationOperation提供了一套简单的多线程编程方法,是IOS多线程编程中最简单的一种实现方式。直接看代码:

[cpp] view plain copy
  1. //创建操作队列  
  2.     NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];  
  3.     //设置队列中最大的操作数  
  4.     [operationQueue setMaxConcurrentOperationCount:1];  
  5.     //创建操作(最后的object参数是传递给selector方法的参数)  
  6.     NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doInBackgroud) object:nil];  
  7.     //将操作添加到操作队列  
  8.     [operationQueue addOperation:operation];  
  9.     [operation release];  
  10.     [operationQueue release];  

运行输出结果跟上面的一样。

0 0
原创粉丝点击