ios多线程的四种方式
- pthread 不怎么使用
- NSThread
- GCD
- NSOperation
NSThread的使用
NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(runThread) object:nil];
[thread setName:@"thread1"];
[thread setThreadPriority:0.2];
[thread start];
[NSThread detachNewThreadSelector:@selector(runThread) toTarget:self withObject:nil];
[self performSelectorInBackground:@selector(runThread) withObject:nil];
[NSThread sleepForTimeInterval:0.5];
@synchronized (self) {
}
GCD的使用
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
dispatch_sync(dispatch_get_main_queue(), ^{
});
});
dispatch_queue_t queue=dispatch_queue_create("com.test.gcd.queue",NULL);
dispatch_async(queue, ^{
});