方法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);
}
上面兩個:
缺點:需要開啓線程
優點:對象可見可以對線程進行操作