有時候可能有很多功能要同時實作,例如每隔多長時間就會檢測程式網絡連接配接,又或者有時候需要從伺服器下載下傳一個不小的檔案,如果用單線程幾乎是不可想的事情,程式将會卡的無法使用,用到多線程和不用多線程,給使用者的體驗天壤之别,是以多線程是一個ios開發人員必須學會的一個知識點。
多線程,聽得有點高深,其實很簡單。在iphone中,多線程可以這麼了解,除主線程外還有其他線程,主線程和其他線程的差別是最重要的,最簡單的了解方法就是主線程會改變界面,其他線程不會改變,主線程可以調用其他線程,
1.聲明一個線程又兩種方法:1.thread1=[[nsthread alloc]initwithtarget:self selector:@selector(tt1) object:nil];這種方法必須啟動 [thread1 start];
2.[nsthread detachnewthreadselector:@selector(tt1) totarget:self withobject:nil];這種方法不需要聲明nsthread對象,也不需要啟動,預設執行到這句就直接運作了
2.當程式在背景執行時,可能有時候需要調用另外的方法,這個方法名是[self performselectorinbackground:@selector(threadmethod) withobject:nil]
3.設定線程優先級-(void)setthreadpriority:(double)priority;線程優先級是從0.0到1.0,1.0是最高優先級。系統預設的優先級是0.5
4.線程休眠:+(void)sleepfortimeinterval:(nstimeinterval)time;time參數為休眠時間。
5.線程的互動:線程的運作過程中,可能需要與其他線程進行通信,如在主線程中修改界面等等,可以使用如下接口:-(void)performselectoronmainthread:(sel)selectoe withobject:(id)arg waituntildone:(bool)wait;
6.線程的同步和線程瑣,可以了解下
7.線程池,可以了解下
用多線程實作一個簡單的程
#import <uikit/uikit.h>
@interface toolbar_1viewcontroller : uiviewcontroller {
uilabel * l1;
uilabel * l2;
uilabel * l3;
uilabel * l4;
uilabel * l5;
uibutton *b1;
uibutton *b2;
uibutton *b3;
uibutton *b4;
uibutton *b5;
nsthread *thread1;
nsthread *thread2;
nsthread *thread3;
nsthread *thread4;
bool button1;
bool button2;
bool button3;
bool button4;
bool button5;
int i1;
int i2;
int i3;
int i4;
int i5;
nscondition *lock;
}
@property (nonatomic,retain)iboutlet uilabel *l1;
@property (nonatomic,retain)iboutlet uilabel *l2;
@property (nonatomic,retain)iboutlet uilabel *l3;
@property (nonatomic,retain)iboutlet uilabel *l4;
@property (nonatomic,retain)iboutlet uilabel *l5;
@property (nonatomic,retain)iboutlet uibutton *b1;
@property (nonatomic,retain)iboutlet uibutton *b2;
@property (nonatomic,retain)iboutlet uibutton *b3;
@property (nonatomic,retain)iboutlet uibutton *b4;
@property (nonatomic,retain)iboutlet uibutton *b5;
-(ibaction)b1:(id)sender;
-(ibaction)b2:(id)sender;
-(ibaction)b3:(id)sender;
-(ibaction)b4:(id)sender;
-(ibaction)b5:(id)sender;
-(void)t1;
-(void)tt1;
-(void)t2;
-(void)tt2;
-(void)t3;
-(void)tt3;
-(void)t4;
-(void)tt4;
-(void)changeall;
@end
#import "toolbar_1viewcontroller.h"
@implementation toolbar_1viewcontroller
@synthesize l1;
@synthesize l2;
@synthesize l3;
@synthesize l4;
@synthesize l5;
@synthesize b1;
@synthesize b2;
@synthesize b3;
@synthesize b4;
@synthesize b5;
-(ibaction)b1:(id)sender
{
thread1=[[nsthread alloc]initwithtarget:self selector:@selector(tt1) object:nil];
button1=!button1;
if(button1){
[b1 settitle:@"停止1" forstate:uicontrolstatenormal] ;
[self changeall];
[thread1 start];
}
else {
[b1 settitle:@"啟動1" forstate:uicontrolstatenormal] ;
[thread1 cancel];
-(void)changeall
if (button1&&button2&button3&button4) {
[b5 settitle:@"停止全部" forstate:uicontrolstatenormal] ;
button5=yes;
if (!button1&&!button2&!button3&!button4) {
[b5 settitle:@"啟動全部" forstate:uicontrolstatenormal] ;
button5=no;
-(void)tt1
while (button1) {
[self performselectoronmainthread:@selector(t1) withobject:nil waituntildone:no];
[nsthread sleepfortimeinterval:1.0];
}
-(void)t1
l1.text=[nsstring stringwithformat:@"%d",++i1];
[lock lock];
i5 ++ ;
[lock unlock];
l5.text=[nsstring stringwithformat:@"%d",i5];
[self.view setneedsdisplay];
-(ibaction)b2:(id)sender
thread2=[[nsthread alloc]initwithtarget:self selector:@selector(tt2) object:nil];
button2=!button2;
if(button2){
[b2 settitle:@"停止2" forstate:uicontrolstatenormal] ;
[thread2 start];
[b2 settitle:@"啟動2" forstate:uicontrolstatenormal] ;
[thread2 cancel];
-(void)tt2
while (button2) {
[self performselectoronmainthread:@selector(t2) withobject:nil waituntildone:no];
-(void)t2
l2.text=[nsstring stringwithformat:@"%d",++i2];
-(ibaction)b3:(id)sender
thread3=[[nsthread alloc]initwithtarget:self selector:@selector(tt3) object:nil];
button3=!button3;
if(button3){
[b3 settitle:@"停止3" forstate:uicontrolstatenormal] ;
[thread3 start];
[b3 settitle:@"啟動3" forstate:uicontrolstatenormal] ;
[thread3 cancel];
-(void)tt3
while (button3) {
[self performselectoronmainthread:@selector(t3) withobject:nil waituntildone:no];
-(void)t3
l3.text=[nsstring stringwithformat:@"%d",++i3];
-(ibaction)b4:(id)sender
thread4=[[nsthread alloc]initwithtarget:self selector:@selector(tt4) object:nil];
button4=!button4;
if(button4){
[b4 settitle:@"停止4" forstate:uicontrolstatenormal] ;
[thread4 start];
[b4 settitle:@"啟動4" forstate:uicontrolstatenormal] ;
[thread4 cancel];
-(void)tt4
while (button4) {
[self performselectoronmainthread:@selector(t4) withobject:nil waituntildone:no];
-(void)t4
l4.text=[nsstring stringwithformat:@"%d",++i4];
-(ibaction)b5:(id)sender
if(!button5){
if (!button1) {
thread1=[[nsthread alloc]initwithtarget:self selector:@selector(tt1) object:nil];
button1=!button1;
[b1 settitle:@"停止1" forstate:uicontrolstatenormal] ;
[thread1 start];
}
if (!button2) {
thread2=[[nsthread alloc]initwithtarget:self selector:@selector(tt2) object:nil];
button2=!button2;
[b2 settitle:@"停止2" forstate:uicontrolstatenormal] ;
[thread2 start];
if (!button3) {
thread3=[[nsthread alloc]initwithtarget:self selector:@selector(tt3) object:nil];
button3=!button3;
[b3 settitle:@"停止3" forstate:uicontrolstatenormal] ;
[thread3 start];
if (!button4) {
thread4=[[nsthread alloc]initwithtarget:self selector:@selector(tt4) object:nil];
button4=!button4;
[b4 settitle:@"停止4" forstate:uicontrolstatenormal] ;
[thread4 start];
else{
if (button1) {
[b1 settitle:@"啟動1" forstate:uicontrolstatenormal] ;
[thread1 cancel];
if (button2) {
[b2 settitle:@"啟動2" forstate:uicontrolstatenormal] ;
[thread2 cancel];
if (button3) {
[b3 settitle:@"啟動3" forstate:uicontrolstatenormal] ;
[thread3 cancel];
if (button4) {
[b4 settitle:@"啟動4" forstate:uicontrolstatenormal] ;
[thread4 cancel];
}
button5=!button5;
- (void)dealloc
[l1 release];
[l2 release];
[l3 release];
[l4 release];
[l5 release];
[b1 release];
[b2 release];
[b3 release];
[b4 release];
[b5 release];
[thread1 release];
[thread2 release];
[thread3 release];
[thread4 release];
[super dealloc];
- (void)viewdidload
i1=0;
button1=false;
button2=false;
button3=false;
button4=false;
button5=false;
i5=i2=i3=i4=i1;
l1.text=[nsstring stringwithformat:@"%d",i1];
l2.text=[nsstring stringwithformat:@"%d",i2];
l3.text=[nsstring stringwithformat:@"%d",i3];
l4.text=[nsstring stringwithformat:@"%d",i4];
xib檔案如下