iOS多线程总结

1.GCD
特点:易用,易读,直观,灵活,高性能
gcd其实没有线程的概念,其实gcd的多线程是由于多任务概念带来的效果
//创建子线程执行dispatch_async(dispatch_get_global_queue(0, 0), ^{ //... });//在主线程执行dispatch_async(dispatch_get_main_queue(), ^{ //... });

2.NSThead

特点:基础,易维护

NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadInMainMethod:) object:nil];
[myThread start];

[NSThread detachNewThreadSelector:@selector(threadInMainMethod:) toTarget:self withObject:nil];

3.NSOperation

特点:系能高,安全可靠,功能强大,控制性强,线程池实现

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^(){
NSLog(@"执行第1次操作,线程:%@", [NSThread currentThread]);
}];

NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^(){
NSLog(@"执行第2次操作,线程:%@", [NSThread currentThread]);
}];
// operation1依赖于operation2
[operation1 addDependency:operation2];

[queue addOperation:operation1];
[queue addOperation:operation2];

4.performSelectorInBackground

特点:易用,清洁,同NSThead

[self performSelectorInBackground:@selector(run) withObject:nil];

你可能感兴趣的:(iOS多线程总结)