足球游戏_中国足彩网¥体育资讯$

iphone中如何进行多线程编程
来源:易贤网 阅读:749 次 日期:2014-12-08 13:44:58
温馨提示:易贤网小编为您整理了“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

import

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

}

如果你什么都不考虑,在线程函数内调用 autorelease 、那么会出现下面的错误:

nsautoreleasenopool(): object 0x********* of class nsconretedata autoreleased with no pool in place ….

四、关于线程池,大家可以查看nsoperation的相关资料

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看技术文章
易贤网手机网站地址:iphone中如何进行多线程编程
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标