Pthreads
#import <pthread.h>
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
pthread_t thread;
pthread_create(&thread, NULL, testfunction, NULL);
}
void *testfunction(void *data) {
NSLog(@"%@", [NSThread currentThread]);
return NULL;
}
//需要手动处理线程的各个状态的转换即管理生命周期
NSThread
// 创建 这是实例方法
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(doSth:) object:nil];
// 启动
[thread start];
//创建并自动启动 这是类方法
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:nil];
NSObject类方法
[Obj performSelectorInBackground:@selector(doSomething) withObject:nil];
[Obj performSelectorOnMainThread:@selector(doSomething) withObject:nil waitUntilDone:Bool];
//还可以更新其他线程:performSelector:onThread:withObject:waitUntilDone: