iOS多线程笔记

多线程

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)重复下载的问题(面试题)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值