方法1:
-(void)test2
{
NSThread *thread=[[NSThread alloc] initWithTarget:self
selector:@selector(run)
object:nil];//参数
//让线程开始工作
[thread start];
}
-(void)run
{
//线程任务
NSLog(@"%@",[NSThread currentThread]);
}
方法2:
//创建线程2
-(void)test3
{
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"heilo"];
}
-(void)run:(NSString *)string
{
//线程任务
NSLog(@"%@,%@",[NSThread currentThread],string);
}
方法3:
-(void)test4
{
//隐式创建线程
[self performSelectorInBackground:@selector(run2:) withObject:@"aa"];
}
-(void)run2:(NSString *)string
{
NSLog(@"%@",string);
}
上面两个:
缺点:需要开启线程
优点:对象可见可以对线程进行操作