建立執行緒有三種方法:
一、通過[nsthread detachnewthreadselector:@selector(addaction) totarget:self withobject:nil]建立,無具體的返回物件,執行緒不受使用者控制,控制權掌握在系統的手中;
二、通過[[nsthread alloc] initwithtarget:self selector:@selector(addnumberto:) object:maxnumber]建立,通過該方式建立的執行緒由使用者自己管理;
三、自定義執行緒,通過繼承nsthread來實現。
//
// viewcontroller.m
// threaddemo
//// created by fox on 12-5-13.
//#import "viewcontroller.h"
#import "mythread.h"
@inte***ce viewcontroller ()
@end
@implementation viewcontroller
- (void)viewdidload
- (void)viewdidunload
- (bool)shouldautorotatetointe***ceorientation:(uiinte***ceorientation)inte***ceorientation
- (void)addaction
nslog(@"子執行緒1執行,the number is:%d",sum);
[pool release];
}- (void)addnumberto:(nsnumber *)maxnumber
nslog(@"子執行緒2執行,the number is:%d",sum);
[pool release];
}@end
自定義的執行緒類:
mythread.h
#import @inte***ce mythread : nsthread
@end
mythread.m
//
// mythread.m
// threaddemo
//// created by fox on 12-5-13.
//#import "mythread.h"
@implementation mythread
//重寫執行緒的入口函式,在此新增執行緒方法
- (void)main
nslog(@"子執行緒3執行,the number is:%d",sum);
[pool release];
}@end
iPhone學習之多執行緒筆記
程序 正在執行的程式 執行緒 是對程序的細化,乙個程序包含一到多個執行緒 多執行緒大大提高了工作效率 多執行緒 concurrent 併發 單執行緒 serid 序列 執行緒同步 任務之間有依賴關係,乙個任務執行完後,另乙個任務才能執行.執行緒互斥 可以用nslock 程序鎖 解決 ui介面重新整理...
iphone開發 多執行緒
from 舉例說明怎麼簡單的建立乙個子執行緒。用到的類是nsthread類,這裡使用detachnewtheadselector totagaet withobject建立乙個執行緒。函式setupthread nsarray userinfor。通過userinfor將需要的資料傳到執行緒中。函式...
iphone開發 多執行緒
舉例說明怎麼簡單的建立乙個子執行緒。用到的類是nsthread類,這裡使用detachnewtheadselector totagaet withobject建立乙個執行緒。函式setupthread nsarray userinfor。通過userinfor將需要的資料傳到執行緒中。函式定義 vo...