-
創(chuàng)建的三種方式
-
第一種
- 介紹
alloc init 創(chuàng)建 第一個(gè)參數(shù):目標(biāo)對(duì)象 第二個(gè)參數(shù):選擇器履怯,線程啟動(dòng)要調(diào)用哪個(gè)方法 第三個(gè)參數(shù):前面方法要接收的參數(shù)(最多只能接收一個(gè)參數(shù)回还,沒(méi)有則傳nil) NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"子線程"];
- 使用
/** * 第一種方式創(chuàng)建 */ -(void)threadCreate1{ NSLog(@"主線程-------%@",[NSThread currentThread]); //創(chuàng)建線程 NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"子線程"]; //開(kāi)始執(zhí)行線程 [thread start]; NSLog(@"主線程-------%@",[NSThread currentThread]); } /** * 線程執(zhí)行方法 * */ -(void)run:(id)obj{ for (int i=0; i<3; i++) { NSLog(@"%@----%@",[NSThread currentThread],obj); } }
-
結(jié)果
-
第二種
- 介紹
/* 分離出一條子線程 第一個(gè)參數(shù):線程啟動(dòng)調(diào)用的方法 第二個(gè)參數(shù):目標(biāo)對(duì)象 第三個(gè)參數(shù):傳遞給調(diào)用方法的參數(shù) */ [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是分離出來(lái)的子線程"];
- 使用
/** * 第二種方式創(chuàng)建 */ -(void)threadCreate2{ NSLog(@"主線程-------%@",[NSThread currentThread]); //detachNewThreadSelector 會(huì)理解在后臺(tái)執(zhí)行selector方法 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"我是分離出來(lái)的子線程"]; NSLog(@"主線程-------%@",[NSThread currentThread]); } /** * 線程執(zhí)行方法 * */ -(void)run:(id)obj{ for (int i=0; i<3; i++) { NSLog(@"%@----%@",[NSThread currentThread],obj); } }
-
結(jié)果
-
第三種
-
介紹
后臺(tái)線程 //第一個(gè)參數(shù):線程啟動(dòng)調(diào)用的方法 //第二個(gè)參數(shù):傳遞給調(diào)用方法的參數(shù) //特點(diǎn):自動(dòng)啟動(dòng)線程 [self performSelectorInBackground:@selector(run:) withObject:@"我是后臺(tái)線程"];
-
使用
/** *第三種方式創(chuàng)建 */ -(void)threadCreate3{ NSLog(@"主線程-------%@",[NSThread currentThread]); //是NSObject分類的一個(gè)方法,意味著所有繼承于NSObject的類都是用用此方法叹洲,并且在其他線程執(zhí)行方法柠硕! //只要設(shè)定,那么它會(huì)立即在后臺(tái)執(zhí)行 selector 方法 [self performSelectorInBackground:@selector(run:) withObject:@"后臺(tái)線程"]; NSLog(@"主線程-------%@",[NSThread currentThread]); } /** *線程執(zhí)行方法 * */ -(void)run:(id)obj{ for (int i=0; i<3; i++) { NSLog(@"%@----%@",[NSThread currentThread],obj); } }
-
結(jié)果
-
-
線程的狀態(tài)
- 介紹
//線程的各種狀態(tài):新建-就緒-運(yùn)行-阻塞-死亡 //常用的控制線程狀態(tài)的方法 [NSThread exit];//退出當(dāng)前線程 [NSThread sleepForTimeInterval:2.0];//阻塞線程 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];//阻塞線程 //注意:線程死了不能復(fù)生
- 使用
/** * 狀態(tài) */ -(void)threadState{ NSLog(@"睡2 秒"); //為類方法运提,直接讓線程睡眠兩秒 [NSThread sleepForTimeInterval:2.0]; //分離出一個(gè)子線程 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil]; } /** * 線程執(zhí)行方法 * */ -(void)run:(id)obj{ for (int i=0; i<8; i++) { if(i==2){ NSLog(@"睡5秒"); //[NSDate dateWithTimeIntervalSinceNow:5.0] 從現(xiàn)在開(kāi)始過(guò)了5秒 //睡到指定的日期蝗柔, [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]]; } NSLog(@"%@----%d",[NSThread currentThread],i); //當(dāng)線程滿足一定的條件,可以強(qiáng)行停止 if (i==5) { //退出 [NSThread exit]; } } NSLog(@"這里是不能來(lái)的民泵,因?yàn)樵?的時(shí)候就退出了"); }
-
結(jié)果
擴(kuò)展
一定不能在主線程執(zhí)行 [NSThread exit] 如果一旦執(zhí)行了那么程序則會(huì)終止掉
-
寫到這才發(fā)現(xiàn)癣丧,忘記介紹線程中常用屬性
//設(shè)置線程的屬性 //設(shè)置線程的名稱 thread.name = @"線程A"; //設(shè)置線程的優(yōu)先級(jí),注意線程優(yōu)先級(jí)的取值范圍為0.0~1.0之間,1.0表示線程的優(yōu)先級(jí)最高,如果不設(shè)置該值栈妆,那么理想狀態(tài)下默認(rèn)為0.5 thread.threadPriority = 1.0;
-
thread.name
是在一般比較大型的項(xiàng)目中比較常見(jiàn)的設(shè)置胁编,因?yàn)榇笮晚?xiàng)目中會(huì)有走很多線程厢钧,然而出現(xiàn)bug,thread.name能在短時(shí)間內(nèi)查找 -
thread.threadPriority
這個(gè)是設(shè)置優(yōu)先級(jí)別掏呼,但是我建議還是不要去設(shè)置它,因?yàn)槎嗑€程開(kāi)發(fā):盡量簡(jiǎn)單點(diǎn)坏快,不要太復(fù)雜,影響性能不說(shuō)憎夷,有可能還把自己帶暈莽鸿,不值得,以上純屬個(gè)人建議拾给。
-
本章到此結(jié)束
歡迎各位碼友隨意轉(zhuǎn)載并指正