码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 线程的总结

时间:2017-03-22 23:05:35      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:自己   gcd   创建   tar   current   ota   art   url   问题   

多线程

1.NSThread

1> 开线程的几种方式
* 先创建,后启动
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];

* 直接启动
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
[self performSelectorInBackground:@selector(run) withObject:nil];

2> 其他用法
NSThread *current = [NSThread currentThread];
+ (NSThread *)mainThread; // 获得主线程

3> 线程间通信
performSelectorOnMainThread.....

2.GCD(重点)
1> 队列的类型
* 并发队列
获得全局的并发队列: dispatch_get_global_queue

* 串行队列
a.自己创建
dispatch_queue_create

b.主队列
dispatch_get_main_queue

2> 执行任务的方法类型
* 同步(sync)执行
* 异步(async)执行

3> 了解队列和方法的配合使用

4> 线程间通信
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   // 执行耗时的异步操作...
   dispatch_async(dispatch_get_main_queue(), ^{
       // 回到主线程,执行UI刷新操作
   });
});

5> 其他用法
dispatch_once
dispatch_after
dispatch_group_async\dispatch_group_notify

3.NSOperation
1> 基本使用
NSInvocationOperation
NSBlockOperation

2> NSOperationQueue(重点)
* 最大并发数设置
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;

* 设置依赖(面试题)
[operationB addDependency:operationA]; // 操作B依赖于操作A

3> 自定义Operation(了解基本流程)

4> 如何解决一张图片(一个url)重复下载的问题(面试题)

IOS 线程的总结

标签:自己   gcd   创建   tar   current   ota   art   url   问题   

原文地址:http://www.cnblogs.com/liuwj/p/6602253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!