1976年贪婪的嘴唇,韩国少妇激三级做爰2023电影,久久久久久精品国产三级涨奶,妖精漫画在线观看免费漫画下拉式

iphone中如何進行多線程編程
來源:易賢網 閱讀:1051 次 日期: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中如何進行多線程編程
由于各方面情況的不斷調整與變化,易賢網提供的所有考試信息和咨詢回復僅供參考,敬請考生以權威部門公布的正式信息和咨詢為準!

2026國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
云南網警備案專用圖標
聯系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網
云南網警報警專用圖標
主站蜘蛛池模板: 黔江区| 桓台县| 广东省| 红原县| 四平市| 哈密市| 防城港市| 凌源市| 当涂县| 平果县| 榆林市| 岢岚县| 泗洪县| 扬中市| 河南省| 来凤县| 宣汉县| 施秉县| 牟定县| 秦安县| 永春县| 莱西市| 台南市| 五家渠市| 永靖县| 贞丰县| 昭平县| 鄢陵县| 岳西县| 嘉义市| 镇康县| 宣汉县| 砀山县| 阳城县| 合阳县| 湖州市| 五台县| 左贡县| 杨浦区| 邓州市| 板桥市|