多线程 总结
2015-10-07 16:17
357 查看
多线程
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)重复下载的问题(面试题)
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)重复下载的问题(面试题)
相关文章推荐
- Cygwin工具的简单使用
- 为了使界面组件更圆滑,Swing,且跨系统
- JSP-注释,脚本元素,指令
- linux下的打包压缩和解压命令
- Microsoft Visual Studio 2013产品激活密钥
- 原码、反码和补码和数据类型的常量
- 最长公共子序列
- hibernate3 配置log4j
- 【进阶】Unity资源解决方案之AssetBundle
- 西安校区来校路线介绍
- 第四周项目1-建立单链表
- Codeforces Round #324 (Div. 2) 数论乱搞过
- Spring MVC+Spring+Hibrenarte实现的简单的CRUD项目实例
- POJ 1017
- 发工资咯:)
- 随机森林算法的使用
- 关于App的一些迷思以及一些动画效果开源库的推荐
- 第6周—项目2 建立链栈算法库
- iOS开发-------MVC架构思想-植物大战僵尸
- 解决popupwindow中弹出输入法被遮挡问题