Objective C IOS 中多執行緒示例

2021-07-09 01:57:09 字數 1415 閱讀 5129

//

初始化鎖物件

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

- (void

)run}//

釋放資源。

- (void

)dealloc

//執行緒在執行過程中,可能需要與其它執行緒進行通訊,如在主線程中修改介面等等,可以使用如下介面:

- (void)performselectoronmainthread:(sel)aselector withobject:(id

)arg waituntildone:(bool)wait

如:[self performselectoronmainthread:@selector(updateui) withobject:nil waituntildone:no];

//updateui為和ui交換的方法名。

//nsautoreleasepool啟用。

使用另一種方法建立後台子執行緒:

//

用到的類是nsthread類,這裡使用detachnewtheadselector:totagaet:withobject建立乙個執行緒。

//函式setupthread:(nsarray*)userinfor。通過userinfor將需要的資料傳到執行緒中。

//函式定義:

-(void)setupthread:(nsarray*)userinfor

- (void)threadfunc:(id

)userinfor

//performselectoronmainthread通知主線程執行函式endthread。也可以使用performselector:onthread:withobject:waituntil 通知某執行緒執行執行緒結束後的處理。

//執行緒內不要重新整理介面。如果需要重新整理介面,通過performselectoronmainthread,調出主線程中的方法去重新整理。

**:

Objective C IOS 中多執行緒示例

初始化鎖物件 ticketcondition nscondition alloc init 開始第乙個執行緒。ticketsthreadone nsthread alloc initwithtarget self selector selector run object nil ticketsthr...

playframework中多對多class

如有user和lesson兩個類,兩者屬於多對多的關係,寫法如 lesson中 屬於很多使用者 public listusers user中 有很多使用者課程 manytomany cascade cascadetype.all,targetentity models.lesson.class pu...

VMware中CentOS配置多網絡卡多IP

1 centos6.5配置網路 講解了 怎麼配置eth0。這一篇講解在虛擬機器中配置eth1。2 首先給eth0 192.168.142.130的虛擬機器,新增第二塊網絡卡裝置。選中虛擬機器,右鍵設定,新增網路介面卡。3 配置。將eth1配置成為內網ip 10.107.1.130。vi etc sy...