多线程在各类编程语言中都是难点,不少语言中实现起来很麻烦,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