IOS学习之多线程(2)--创建线程
轉(zhuǎn)載自?http://www.cnblogs.com/wendingding/p/3805119.html
一、創(chuàng)建和啟動(dòng)線程簡(jiǎn)單說(shuō)明
一個(gè)NSThread對(duì)象就代表一條線程
創(chuàng)建、啟動(dòng)線程
(1) NSThread?*thread = [[NSThread?alloc]?initWithTarget:self?selector:@selector(run)?object:nil];
[thread?start];
//?線程一啟動(dòng),就會(huì)在線程thread中執(zhí)行self的run方法
?
主線程相關(guān)用法
+ (NSThread?*)mainThread;?//?獲得主線程
- (BOOL)isMainThread;?//?是否為主線程
+ (BOOL)isMainThread;?//?是否為主線程
?
其他用法
獲得當(dāng)前線程
NSThread?*current = [NSThread?currentThread];
?
線程的調(diào)度優(yōu)先級(jí):調(diào)度優(yōu)先級(jí)的取值范圍是0.0 ~ 1.0,默認(rèn)0.5,值越大,優(yōu)先級(jí)越高
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
?
設(shè)置線程的名字
- (void)setName:(NSString?*)n;
- (NSString?*)name;
?
其他創(chuàng)建線程的方式
(2)創(chuàng)建線程后自動(dòng)啟動(dòng)線程???[NSThread?detachNewThreadSelector:@selector(run)?toTarget:self?withObject:nil];
(3)隱式創(chuàng)建并啟動(dòng)線程??[self?performSelectorInBackground:@selector(run)?withObject:nil];
上述2種創(chuàng)建線程方式的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):簡(jiǎn)單快捷
缺點(diǎn):無(wú)法對(duì)線程進(jìn)行更詳細(xì)的設(shè)置
二、代碼示例
1.使用古老的方式創(chuàng)建
// //??YYViewController.m // // //??Created?by?apple?on?14-6-23. //??Copyright?(c)?2014年?itcase.?All?rights?reserved. // #import?"YYViewController.h" #import?<pthread.h> @interface?YYViewController?() -?(IBAction)btnClick; @end@implementation?YYViewController-?(void)viewDidLoad {[super?viewDidLoad]; }//按鈕的點(diǎn)擊事件 -?(IBAction)btnClick?{//1.獲取當(dāng)前線程N(yùn)SThread?*current=[NSThread?currentThread];//主線程N(yùn)SLog(@"btnClick----%@",current);???//2.使用for循環(huán)執(zhí)行一些耗時(shí)操作pthread_t?thread;pthread_create(&thread,?NULL,?run,?NULL); } //c語(yǔ)言函數(shù) void?*run(void?*data) {//獲取當(dāng)前線程,是新創(chuàng)建出來(lái)的線程N(yùn)SThread?*current=[NSThread?currentThread];for?(int?i=0;?i<10000;?i++)?{NSLog(@"btnClick---%d---%@",i,current);}return?NULL; } //多個(gè)線程,點(diǎn)擊按鈕執(zhí)行按鈕調(diào)用方法的時(shí)候,主線程沒(méi)有被阻塞 @end實(shí)現(xiàn)效果:
?
打印結(jié)果:
2.使用NSThread創(chuàng)建線程
// //??YYViewController.m // // //??Created?by?apple?on?14-6-23. //??Copyright?(c)?2014年?itcase.?All?rights?reserved. // #import?"YYViewController.h" #import?<pthread.h> @interface?YYViewController?() -?(IBAction)btnClick; @end @implementation?YYViewController? -?(void)viewDidLoad {[super?viewDidLoad]; } //按鈕的點(diǎn)擊事件 -?(IBAction)btnClick?{//1.獲取當(dāng)前線程N(yùn)SThread?*current=[NSThread?currentThread];//主線程N(yùn)SLog(@"btnClick----%@",current);//獲取主線程的另外一種方式NSThread?*main=[NSThread?mainThread];NSLog(@"主線程-------%@",main);//2.執(zhí)行一些耗時(shí)操作[self?creatNSThread]; //????[self?creatNSThread2]; //????[self?creatNSThread3]; }/***?NSThread創(chuàng)建線程方式1*?1>?先創(chuàng)建初始化線程*?2>?start開啟線程*/ -(void)creatNSThread {NSThread??*thread=[[NSThread?alloc]initWithTarget:self?selector:@selector(run:)?object:@"線程A"];//為線程設(shè)置一個(gè)名稱thread.name=@"線程A";//開啟線程[thread?start];NSThread??*thread2=[[NSThread?alloc]initWithTarget:self?selector:@selector(run:)?object:@"線程B"];//為線程設(shè)置一個(gè)名稱thread2.name=@"線程B";//開啟線程[thread2?start]; }/***?NSThread創(chuàng)建線程方式2 *創(chuàng)建完線程直接(自動(dòng))啟動(dòng)*/ -(void)creatNSThread2 { //????NSThread?*thread=[NSThread?detachNewThreadSelector:@selector(run:)?toTarget:self?withObject:@"創(chuàng)建完線程直接(自動(dòng))啟動(dòng)"];[NSThread?detachNewThreadSelector:@selector(run:)?toTarget:self?withObject:@"創(chuàng)建完線程直接(自動(dòng))啟動(dòng)"]; } /***?NSThread創(chuàng)建線程方式3*?隱式創(chuàng)建線程,?并且直接(自動(dòng))啟動(dòng)*/ -(void)creatNSThread3 {//在后臺(tái)線程中執(zhí)行===在子線程中執(zhí)行[self?performSelectorInBackground:@selector(run:)?withObject:@"隱式創(chuàng)建"]; } -(void)run:(NSString?*)str {//獲取當(dāng)前線程N(yùn)SThread?*current=[NSThread?currentThread];//打印輸出for?(int?i=0;?i<10;?i++)?{NSLog(@"run---%@---%@",current,str);} } @end調(diào)用線程1,打印結(jié)果為:
調(diào)用線程2?
調(diào)用線程3?
轉(zhuǎn)載于:https://blog.51cto.com/5934497/1718198
總結(jié)
以上是生活随笔為你收集整理的IOS学习之多线程(2)--创建线程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 42.对话框插件——dialog
- 下一篇: 当 IDENTITY_INSERT 设置