iOS NSThread基本使用

#import"ViewController.h"

#import"XMGThread.h"

@interfaceViewController()

@end

@implementationViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

{

[selfcreateNewThread1];

}

//1.alloc init创建线程,需要手动启动线程

//线程的生命周期:当任务执行完毕之后被释放掉

-(void)createNewThread1

{

//1.创建线程

/*

第一个参数:目标对象self

第二个参数:方法选择器调用的方法

第三个参数:前面调用方法需要传递的参数nil

*/

XMGThread*threadA = [[XMGThreadalloc]initWithTarget:selfselector:@selector(run:)object:@"ABC"];

//设置属性

threadA.name=@"线程A";

//设置优先级取值范围0.0 ~ 1.0之间最高是1.0默认优先级是0.5

threadA.threadPriority=1.0;

//2.启动线程

[threadAstart];

//NSThread *threadB = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];

//threadB.name = @"线程b";

//threadB.threadPriority = 0.1;

//[threadB start];

//

//NSThread *threadC = [[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"ABC"];

//threadC.name = @"线程C";

//[threadC start];

}

//2.分离子线程,自动启动线程,

-(void)createNewThread2

{

[NSThreaddetachNewThreadSelector:@selector(run:)toTarget:selfwithObject:@"分离子线程"];

}

//3.开启一条后台线程

-(void)createNewThread3

{

[selfperformSelectorInBackground:@selector(run:)withObject:@"开启后台线程"];

}

-(void)run:(NSString*)param

{

//NSLog(@"---run----%@---%@",[NSThread currentThread].name,param);

for(NSIntegeri =0; i<10000; i++) {

NSLog(@"%zd----%@",i,[NSThreadcurrentThread].name);

}


iOS NSThread基本使用_第1张图片
iOS NSThread基本使用_第2张图片

你可能感兴趣的:(iOS NSThread基本使用)