// 建立、启动线程 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; [thread start]; // 线程一启动,就会在线程thread中执行self的run方法 // 主线程相关用法 + (NSThread *)mainThread; // 得到主线程 - (BOOL)isMainThread; // 是否为主线程 + (BOOL)isMainThread; // 是否为主线程
// 得到当前线程 NSThread *current = [NSThread currentThread]; // 线程的名字 - (void)setName:(NSString *)n; - (NSString *)name;
// 建立线程后自动启动线程 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil]; // 隐式建立并启动线程 [self performSelectorInBackground:@selector(run) withObject:nil]; /* * 上述2种建立线程方式的优缺点 * 优势:简单快捷 * 缺点:没法对线程进行更详细的设置 */