2010-09-01 54 views

回答

0

查阅关于NSThread,NSOperationQueue的文档以及为NSObject定义的各种performSelector...方法。

+0

谢谢你的回答,这可能会帮助我... – sandeep 2010-09-03 06:14:37

0

NSThread Class reference

一个例子来运行一个子线程:

- (void)threadRun 
{ 
    // One thread, one autorelease pool. as main thread's pool inside main.c 
    NSAutoreleasePool *aPool = [[NSAuroreleasePool alloc] init]; 

    // do-job in thread here 

    [aPool release]; 
} 

- (void)performJobWithThread 
{ 
    // Method 1: 
    NSThread *aThread = [[NSThread alloc] initWithTarget:self 
              selector:@selector(threadRun) 
              object:nil]; 

    [aThread start]; 

    /// Release aThread at some point avoid memory leak. 

    // Method 2: 
    [NSThread detachNewThreadSelector:@selector:@selector(threadRun) 
          toTarget:self 
          withObject:nil]; 
} 

使用NSThread之前,有更好的阅读Threading Programming Guide第一。它会告诉你关于内存管理,与其他线程的通信,...等。

NSOperation和NSOperationQueue很好地设计多线程。但我现在正在学习它们,我无法清楚地谈论它们。