标签:
longOperation:- (void)longOperation:(id)obj {
NSLog(@"%@ - %@", [NSThread currentThread], obj);
}
- (void)threadDemo1 {
NSLog(@"before %@", [NSThread currentThread]);
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(longOperation:) object:@"THREAD"];
[thread start];
NSLog(@"after %@", [NSThread currentThread]);
}
[thread start];执行后,会在另外一个线程执行 longOperation: 方法block除外)- (void)threadDemo2 {
NSLog(@"before %@", [NSThread currentThread]);
[NSThread detachNewThreadSelector:@selector(longOperation:) toTarget:self withObject:@"DETACH"];
NSLog(@"after %@", [NSThread currentThread]);
}
detachNewThreadSelector 类方法不需要启动,会自动创建线程并执行 @selector 方法- (void)threadDemo3 {
NSLog(@"before %@", [NSThread currentThread]);
[self performSelectorInBackground:@selector(longOperation:) withObject:@"PERFORM"];
NSLog(@"after %@", [NSThread currentThread]);
}
performSelectorInBackground 是 NSObject 的分类方法@selector 方法thread 字眼,隐式创建并启动线程NSObject 都可以使用此方法,在其他线程执行方法标签:
原文地址:http://www.cnblogs.com/Ruby-Hua/p/5150028.html