NSThread 多线程相关

1.下面的代码,有2点需要注意,1>就是 就是thread:所传得参数,这里传得的是nsarray  当然也可以传其他的类型。2>
[self performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:YES]; 这个函数的作用是通知主线程进行一下操作,比如这里是更新btn 的title。主要的参数需要注意的是 waitUntilDone ,如果是YES  那就需要等到 update操作完之后才会执行NSLog(@"-------------ddddd-----------");以及以后的代码,如果设置为NO的话,那不需要等到update完成就会执行。



NSArray * array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil]; therad = [[NSThread alloc]initWithTarget:self selector:@selector(thread:) object:array]; [therad start]; -(void)update { [btn setTitle:@"bbbbb" forState:UIControlStateNormal]; for( int i = 0; i < 100; ++ i ) { NSLog(@"i:%d",i); } } -(void)thread:(NSArray *)array { for( int i = 0; i < 3; ++ i ) { sleep(1); NSLog(@"%@",[array objectAtIndex:i]); } [self performSelectorOnMainThread:@selector(update) withObject:nil waitUntilDone:YES]; NSLog(@"-------------ddddd-----------"); }
原文地址:https://www.cnblogs.com/rollrock/p/4311835.html