多線程在各類編程語言中都是難點,不少語言中實現起來很麻煩,objective-c雖然源於c,但其多線程編程卻至關簡單,能夠與java相媲美。這篇 文章主要從線程建立與啓動、線程的同步與鎖、線程的交互、線程池等等四個方面簡單的講解一下iphone中的多線程編程。java
1、線程建立與啓動
線程建立主要有二種方式:objective-c
1 2 |
- (id)init; // designated initializer - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument; |
固然,還有一種比較特殊,就是使用所謂的convenient method,這個方法能夠直接生成一個線程並啓動它,並且無需爲線程的清理負責。這個方法的接口是:編程
1 |
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument |
前兩種方法建立後,須要手機啓動,啓動的方法是:安全
- (void)start;多線程
2、線程的同步與鎖
要說明線程的同步與鎖,最好的例子可能就是多個窗口同時售票的售票系統了。咱們知道在java中,使用synchronized來同步,而iphone雖 然沒有提供相似java下的synchronized關鍵字,但提供了NSCondition對象接口。查看NSCondition的接口說明能夠看 出,NSCondition是iphone下的鎖對象,因此咱們可使用NSCondition實現iphone中的線程安全。這是來源於網上的一個例 子:
SellTicketsAppDelegate.h 文件app
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// 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 文件iphone
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
// 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 |
3、線程的交互
線程在運行過程當中,可能須要與其它線程進行通訊,如在主線程中修改界面等等,可使用以下接口:編程語言
1 |
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait |
因爲在本過程當中,可能須要釋放一些資源,則須要使用NSAutoreleasePool來進行管理,如:ide
1 2 3 4 5 6 7 |
- (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 ….函數
4、關於線程池,你們能夠查看NSOperation的相關資料。
原文連接:http://www.voland.com.cn/iphone-in-the-multi-threaded-programming