object-c多线程

来源:互联网 发布:金融互换 知乎 编辑:程序博客网 时间:2024/05/21 17:59
object-c的多线程如java的多线程一样方便可靠。

一、线程创建与启动
  线程创建主要有二种方式:
[cpp] view plaincopyprint?
  1. - (id)init; // designated initializer  
  2. - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;  

  当然,还有一种比较特殊,就是使用所谓的convenient method,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。这个方法的接口是:  
[cpp] view plaincopyprint?
  1. + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument  

  前两种方法创建后,需要手机启动,启动的方法是:
[cpp] view plaincopyprint?
  1. - (void)start;  

  二、线程的同步与锁
  要说明线程的同步与锁,最好的例子可能就是多个窗口同时售票的售票系统了。我们知道在java中,使用synchronized来同步,而iphone虽然没有提供类似java下的synchronized关键字,但提供了NSCondition对象接口。查看NSCondition的接口说明可以看出,NSCondition是iphone下的锁对象,所以我们可以使用NSCondition实现iphone中的线程安全。这是来源于网上的一个例子:

SellTicketsAppDelegate.h 文件
[cpp] view plaincopyprint?
  1. // SellTicketsAppDelegate.h  
  2. import   
  3. @interface SellTicketsAppDelegate : NSObject {  
  4. int tickets;  
  5. int count;  
  6. NSThread* ticketsThreadone;  
  7. NSThread* ticketsThreadtwo;  
  8. NSCondition* ticketsCondition;  
  9. UIWindow *window;  
  10. }  
  11. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  12. @end  

SellTicketsAppDelegate.m 文件
[cpp] view plaincopyprint?
  1.    // SellTicketsAppDelegate.m  
  2. import "SellTicketsAppDelegate.h"  
  3. @implementation SellTicketsAppDelegate  
  4. @synthesize window;  
  5. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  6.   
  7. tickets = 100;  
  8. count = 0;  
  9. // 锁对象  
  10. ticketCondition = [[NSCondition alloc] init];  
  11. ticketsThreadone = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  12. [ticketsThreadone setName:@"Thread-1"];  
  13. [ticketsThreadone start];  
  14. ticketsThreadtwo = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];  
  15. [ticketsThreadtwo setName:@"Thread-2"];  
  16. [ticketsThreadtwo start];  
  17. //[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];  
  18. // Override point for customization after application launch  
  19. [window makeKeyAndVisible];  
  20. }  
  21. - (void)run{  
  22. while (TRUE) {  
  23. // 上锁  
  24. [ticketsCondition lock];  
  25. if(tickets > 0){  
  26. [NSThread sleepForTimeInterval:0.5];  
  27. count = 100 - tickets;  
  28. NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);  
  29. tickets--;  
  30. }else{  
  31. break;  
  32. }  
  33. [ticketsCondition unlock];  
  34. }  
  35. }  
  36. - (void)dealloc {  
  37. [ticketsThreadone release];  
  38. [ticketsThreadtwo release];  
  39. [ticketsCondition release];  
  40. [window release];  
  41. [super dealloc];  
  42. }  
  43. @end  

  三、线程的交互
  线程在运行过程中,可能需要与其它线程进行通信,如在主线程中修改界面等等,可以使用如下接口:
[cpp] view plaincopyprint?
  1. - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait  

  由于在本过程中,可能需要释放一些资源,则需要使用NSAutoreleasePool来进行管理,如:
[cpp] view plaincopyprint?
  1. - (void)startTheBackgroundJob {  
  2. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  3. // to do something in your thread job  
  4. ...  
  5. [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];  
  6. [pool release];  
  7. }