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…
完整代碼如下:
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- NSLog(@"Thread is start...");
- //使用類方法創建線程
- // [NSThread detachNewThreadSelector:@selector(doInBackgroud) toTarget:self withObject:nil];
- //使用傳統方式創建
- NSThread *thread = [[NSThreadalloc] initWithTarget:self selector:@selector(doInBackgroud) object:nil];
- [thread start];
- [thread release];
- }
- //在另一個線程中運行的方法
- -(void)doInBackgroud
- {
- NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
- //do someting...
- NSLog(@"Thread is running...");
- [NSThread sleepForTimeInterval:3];
- NSLog(@"Thread is done...");
- [releasePool release];
- }
二、NSOperation
NSOperation是Cocoa中的一個抽象類,用來封裝單個任務和代碼執行一項操作,由於是抽象類,所以不能直接實例化使用,必須定義子類繼承該抽象類來實現,比較常用的NSOperation的子類有NSInvocationOperation,另外,也可以自己繼承NSOperation來實現線程的操作。
另外會使用到操作隊列NSOperationQueue,它相當於一個線程隊列或者可以叫做線程池,可以順序執行隊列中的操作,也可以設置隊列中操作的優先級。
.h頭文件
- #import <Foundation/Foundation.h>
- @interface MyTaskOperation : NSOperation
- @end
.m實現文件:
- #import "MyTaskOperation.h"
- @implementation MyTaskOperation
- //相當於Java線程中的run方法
- -(void)main
- {
- //do someting...
- NSLog(@"Thread is running...");
- [NSThreadsleepForTimeInterval:3];
- NSLog(@"Thread is done...");
- }
- @end
使用方法如下:
- //使用NSOperation子類來創建線程
- NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
- MyTaskOperation *myTask = [[MyTaskOperation alloc] init];
- [operationQueue addOperation:myTask];
- [myTask release];
- [operationQueue release];
運行結果和上面結果一樣。
三、NSInvocationOperation
NSOperation的子類NSInvocationOperation提供了一套簡單的多線程編程方法,是IOS多線程編程中最簡單的一種實現方式。直接看代碼:
- //創建操作隊列
- NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
- //設置隊列中最大的操作數
- [operationQueue setMaxConcurrentOperationCount:1];
- //創建操作(最後的object參數是傳遞給selector方法的參數)
- NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doInBackgroud) object:nil];
- //將操作添加到操作隊列
- [operationQueue addOperation:operation];
- [operation release];
- [operationQueue release];
運行輸出結果跟上面的一樣。