iPhone 中的線程應用並不是無節制的,官方給出的資料顯示iPhone OS下的主線程的堆棧大小是1M,第二個線程開始都是512KB。並且該值不能通過編譯器開關或線程API函數來更改。
只有主線程有直接修改UI的能力。
一、 NSOperation和NSOperationQueue
1、一個繼承自 NSOperation的操作類,該類的實現中必須有 - (void)main方法的。
2、使用NSOperation的最簡單方法就是將其放入NSOperationQueue中。
一旦一個操作被加入隊列,該隊列就會啟動並開始處理它(即調用該操作類的main方法)。一旦該操作完成隊列就會釋放它。
<style>< !-- p.p1 {margin:0.0px 0.0px 0.0px 0.0px; font:11.0px Menlo; color:#7e1aad} p.p2 {margin:0.0px 0.0px 0.0px 0.0px; font:11.0px Menlo; color:#428288} p.p3 {margin:0.0px 0.0px 0.0px 0.0px; font:11.0px Menlo; color:#265a5e} p.p4 {margin:0.0px 0.0px 0.0px 0.0px; font:11.0px Menlo} p.p5 {margin:0.0px 0.0px 0.0px 0.0px; font:11.0px Menlo; color:#490085} span.s1 {color:#cb00a5} span.s2 {color:#000000} span.s3 {color:#428288} span.s4 {color:#490085} span.s5 {color:#7e1aad} span.s6 {color:#265a5e} span.s7 {color:#e00005} span.Apple-tab-span {white-space:pre} --></style>self.queue = [[NSOperationQueuealloc] init];
ArticleParseOperation *parser = [[ArticleParseOperationalloc] initWithData:filePathdelegate:self];
[queue addOperation:parser];
[parser release];
[queuerelease];
3、可以給操作隊列設置最多同事運行的操作數: [queue setMaxConcurrentOperationCount:2];
二、NSThread<轉>
一、線程創建與啟動 線程創建主要有二種方式:
- (id)init; // designated initializer - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
當然,還有一種比較特殊,就是使用所謂的convenient method,這個方法可以直接生成一個線程並啟動它,而且無需為線程的清理負責。這個方法的接口是:
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
前兩種方法創建後,需要手機啟動,啟動的方法是:
- (void)start;
二、線程的同步與鎖 要說明線程的同步與鎖,最好的例子可能就是多個窗口同時售票的售票系統了。我們知道在java中,使用synchronized來同步,而iphone雖然沒有提供類似java下的synchronized關鍵字,但提供了NSCondition對象接口。查看NSCondition的接口說明可以看出,NSCondition是iphone下的鎖對象,所以我們可以使用NSCondition實現iphone中的線程安全。這是來源於網上的一個例子: SellTicketsAppDelegate.h 文件
// SellTicketsAppDelegate.h
import <UIKit/UIKit.h>
@interface SellTicketsAppDelegate : NSObject <UIApplicationDelegate> {
int tickets;
int count;
NSThread* ticketsThreadone;
NSThread* ticketsThreadtwo;
NSCondition* ticketsCondition;
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
SellTicketsAppDelegate.m 文件
// SellTicketsAppDelegate.m
import "SellTicketsAppDelegate.h"
@implementation SellTicketsAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tickets = 100;
count = 0;
// 鎖對象
ticketCondition = [[NSCondition alloc] init];
ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[ticketsThreadone setName:@"Thread-1"];
[ticketsThreadone start];
ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[ticketsThreadtwo setName:@"Thread-2"];
[ticketsThreadtwo start];
//[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)run{
while (TRUE) {
// 上鎖
[ticketsCondition lock];
if(tickets > 0){
[NSThread sleepForTimeInterval:0.5];
count = 100 - tickets;
NSLog(@"當前票數是:%d,售出:%d,線程名:%@",tickets,count,[[NSThread currentThread] name]);
tickets--;
}else{
break;
}
[ticketsCondition unlock];
}
}
- (void)dealloc {
[ticketsThreadone release];
[ticketsThreadtwo release];
[ticketsCondition release];
[window release];
[super dealloc];
}
@end
三、線程的交互 線程在運行過程中,可能需要與其它線程進行通信,如在主線程中修改界面等等,可以使用如下接口:
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
由於在本過程中,可能需要釋放一些資源,則需要使用NSAutoreleasePool來進行管理,如:
- (void)startTheBackgroundJob {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// to do something in your thread job
...
[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
[pool release];
}
如果你什麼都不考慮,在線程函數內調用 autorelease 、那麼會出現下面的錯誤: NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place ….
四、關於線程池,大家可以查看NSOperation的相關資料。