多线程之NSThread
- (IBAction)btn:(UIButton *)sender {
// 获得当前线程
NSLog(@"btn = %@",[NSThread currentThread]);
// 1.初始化方法创建线程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
// 1.2. 给线程取名字
thread.name = @"threadA";
// 1.3 开始线程
[thread start];
// 2. 类方法创建线程
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
// 3. 创建隐身线程
[self performSelectorInBackground:@selector(run) withObject:nil];
// 4. 主线程运行
[self run];
}
- (void)run
{
for (int i= 0; i < 10000; i++) {
NSLog(@"%@---> %i",[NSThread currentThread],i);
}
}