IOS多線程之NSThread、NSOperation、NSInvocationOperation

http://blog.csdn.net/tangren03/article/details/7842903

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…


完整代碼如下

  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頭文件

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface MyTaskOperation : NSOperation  
  4.   
  5. @end  

.m實現文件:

  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  

使用方法如下:

  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多線程編程中最簡單的一種實現方式。直接看代碼:

  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];  

運行輸出結果跟上面的一樣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章