iOS多线程实现2-NSThread

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

NSThread是轻量级的多线程开发,OC语言编写,更加面向对象,使用起来也并不复杂,但是使用NSThread需要自己管理线程生命周期。在iOS开发中很少使用它来创建一个线程,但是经常使用它做一些延时操作,获取当前线程,线程间通讯等等。

但是,在线程同步方面,控制线程执行顺序比较麻烦,线程同步对数据的加锁会有一定的系统开销,且创建线程也会增加系统的开销。

1 创建方法

有多种创建方法,- (void)runDemo:(NSString *)param;为要执行的示例方法。

- (void)runDemo:(NSString *)param {

NSThread*current =[NSThread currentThread];

NSLog(@"%@---%@ is running", param, current);

}///方式1 自动创建线程, 并且自动启动- (void)threadCreateOne {//在另一个线程执行 runDemo:[self performSelectorInBackground:@selector(runDemo:) withObject:@"One"];

}///方式2 创建完线程直接(自动)启动- (void)threadCreateTwo {

[NSThread detachNewThreadSelector:@selector(runDemo:) toTarget:self withObject:@"Two"];

}///方式3 先创建初始化线程,然后start开启线程- (void)threadCreateThree {

NSThread*thread = [[NSThread alloc] initWithTarget:self selector:@selector(runDemo:)object:@"Three"];//可以设置线程名字thread.name =@"名字";//开启线程[thread start];

}

下面为测试代码,以及打印结果,我们调用的顺序是One->Two->Three,但是打印结果是Two->Three->One,因为线程启动后仅仅处于就绪状态,实际是否执行要由CPU根据当前状态调度,即执行顺序是无序的,这也是多线程的特点。

///点击屏幕后创建线程- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[self threadCreateOne];

[self threadCreateTwo];

[self threadCreateThree];

}

打印结果:2015-08-2716:27:34.97401test[1183:76667] Two---<NSThread:0x7ff250e1c9a0>{number =3, name = (null)}isrunning2015-08-2716:27:34.97401test[1183:76668] Three---<NSThread:0x7ff250e168a0>{number =4, name = 名字}isrunning2015-08-2716:27:34.97401test[1183:76666] One---<NSThread:0x7ff250f406a0>{number =2, name = (null)}isrunning

2 常用函数

获取当前线程,获取主线程,判断当前线程是否为主线程。

//获取当前线程NSThread *current =[NSThread currentThread];//获取主线程current =[NSThread mainThread];//判断当前线程是否为主线程BOOL isMain = [current isMainThread];

暂停线程,下面代码为2种方法均让当前线程睡5s

[NSThread sleepForTimeInterval:5];

NSDate*date = [NSDate dateWithTimeInterval:5sinceDate:[NSDate date]];

[NSThread sleepUntilDate:date];

获取线程的状态,分别为:正在执行、已经完成、已经取消。

@PRoperty (readonly, getter=isExecuting) BOOL executing;

@property (readonly, getter=isFinished) BOOL finished;

@property (readonly, getter=isCancelled) BOOL cancelled;

在指定的线程(已存在的线程)、主线程、当前线程上执行方法。这种比较常用,通常用于线程间通讯,且它们是NSObject的扩展方法,使用起来很方便。

//在指定的线程执行runDemo:方法,最后的YES代表:下面的代码会阻塞,等runDemo:方法在thread线程执行完毕后,才会执行下面代码的下一行代码,设为NO则不阻塞。那么runDemo:与下一行代码的执行顺序不确定[self performSelector:@selector(runDemo:) onThread:thread withObject:nil waitUntilDone:YES];//在主线程执行runDemo:方法,YES参数同上[self performSelectorOnMainThread:@selector(runDemo:) withObject:nil waitUntilDone:YES];//在当前线程执行方法[self performSelector:@selector(run) withObject:nil];

退出线程

+ (void)exit;

线程优先级相关,优先级范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高。开发时,很少使用优先级,如果设置优先级且使用线程锁会造成优先级翻转,需要特备注意。

+ (double)threadPriority;+ (BOOL)setThreadPriority:(double)p;

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航