關(guān)于iPhone多線程編程的教程
iPhone多線程編程的教程是本文要介紹的內(nèi)容,相信大家也都接觸過多線程的使用,那么本文也可以作為一個(gè)參考來進(jìn)行學(xué)習(xí)。不多說,我們來看內(nèi)容中詳細(xì)講解多線程編程。
iphone中多線程編程:線程的創(chuàng)建
多線程在各種編程語言中都是難點(diǎn),很多語言中實(shí)現(xiàn)起來很麻煩,objective-c雖然源于c,但其多線程編程卻相當(dāng)簡單,可以與java相媲美。
一、線程創(chuàng)建與啟動(dòng)
線程創(chuàng)建主要有二種方式:
- - (id)init; // designated initializer
- - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
當(dāng)然,還有一種比較特殊,就是使用所謂的convenient method,這個(gè)方法可以直接生成一個(gè)線程并啟動(dòng)它,而且無需為線程的清理負(fù)責(zé)。這個(gè)方法的接口是:
- + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
前兩種方法創(chuàng)建后,需要手機(jī)啟動(dòng),啟動(dòng)的方法是:
- - (void)start;
iphone中多線程編程:線程的同步與鎖
要說明線程的同步與鎖,最好的例子可能就是多個(gè)窗口同時(shí)售票的售票系統(tǒng)了。我們知道在java中,使用synchronized來同步,而iphone雖然沒有提供類似java下的synchronized關(guān)鍵字,但提供了NSCondition對象接口。查看NSCondition的接口說明可以看出,NSCondition是iphone下的鎖對象,所以我們可以使用NSCondition實(shí)現(xiàn)iphone中的線程安全。這是來源于網(wǎng)上的一個(gè)例子:
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.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āng)前票數(shù)是:%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
iphone中多線程編程:線程的交互
線程在運(yùn)行過程中,可能需要與其它線程進(jìn)行通信,如在主線程中修改界面等等,可以使用如下接口:
- - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
由于在本過程中,可能需要釋放一些資源,則需要使用NSAutoreleasePool來進(jìn)行管理,如:
- - (void)startTheBackgroundJob {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- // to do something in your thread job
- ...
- [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
- [pool release];
- }
如果你什么都不考慮,在線程函數(shù)內(nèi)調(diào)用 autorelease 、那么會(huì)出現(xiàn)下面的錯(cuò)誤:
- NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place ….
小結(jié):關(guān)于iPhone多線程編程的教程的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!