只有主线程有直接修改UI的能力。
一、 NSOperation和NSOperationQueue
1、一个继承自 NSOperation的操作类,该类的实现中必须有 - (void)main方法的。
2、使用NSOperation的最简单方法就是将其放入NSOperationQueue中。
一旦一个操作被加入队列,该队列就会启动并开始处理它(即调用该操作类的main方法)。一旦该操作完成队列就会释放它。
- self.queue = [[NSOperationQueuealloc] init];
- ArticleParseOperation *parser = [[ArticleParseOperationalloc] initWithData:filePathdelegate:self];
- [queue addOperation:parser];
- [parser release];
- [queuerelease];
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;
- (id)init; // designated initializer
- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
- + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
- <span>-</span> <span>(</span><span>void</span><span>)</span>start<span>;</span>
二、线程的同步与锁
要说明线程的同步与锁,最好的例子可能就是多个窗口同时售票的售票系统了。我们知道在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.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
- 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
// 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
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
- - (void)startTheBackgroundJob {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- // to do something in your thread job
- ...
- [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
- [pool release];
- }
- (void)startTheBackgroundJob {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// to do something in your thread job
...
[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
[pool release];
}
NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place ….
四、关于线程池,大家可以查看NSOperation的相关资料。
原帖: http://www.voland.com.cn/iphone-in-the-multi-threaded-programming