Objective-c多線程 學習

 iPhone 多線程

  多線程在各種編程語言中都是難點,很多語言中實現起來很麻煩,objective-c雖然源於c,但其多線程編程卻相當簡單,可以與java相媲美。這篇文章主要從線程創建與啓動、線程的同步與鎖、線程的交互、線程池等等四個方面簡單的講解一下iphone中的多線程編程。

 

  一、線程創建與啓動

     線程創建主要有二種方式:

                  - (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

 

  @interface SellTicketsAppDelegate : NSObject {

  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];

  }

 

 

此文章轉載至:http://o0o0o0o.javaeye.com/blog/752846

 

 

-------------------------------------------------------------

 

 

 

 

XXXXX nsthread autoreleased with no pool in place - just leaking

這是一個會經常發生的警告提示。

當調用

 

[NSThread detachNewThreadSelector:@selector(XXX) toTarget:self withObject:nil];

發起一個多線程的時候會發生這種警告,那麼,加上NSAutoreleasePool就可以了,NSAutoreleasePool我的理解是一個自動的進程管理池,當然並不代表着有Java或者AS3的強大GC機制。

 

[NSThread detachNewThreadSelector:@selector(ooxx) toTarget:self withObject:nil];  - (void)ooxx { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  //OOXX,if you like:)  [pool release]; }

關於NSAutoreleasePool可以查看Apple 的官方文檔:NSAutoreleasePool Class Reference

另外,需要注意的是,在多進程中要操作主進程的UI是不可以直接操作的,一定要使用:performSelectorOnMainThread

 

此文章轉載至:http://www.isdada.com/cocoa-nsthread-autoreleased-with-no-pool-in-place-just-leaking-issue.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章