多线程相关的话题是面试过程中必不可少的话题。有些面试官可能是要你自己谈谈对多线程的认识,而有些则是出道题给你,让你直接手写或者让你口述实现。
谈谈对多线程的认识
在OC中实现多线程的方法有3种(NSThread
、GCD
、NSOperation
),
NSThread
是苹果官方提供的,可以直接操作线程对象。不过需要程序员自己管理线程的生命周期(主要是创建),所以偶尔用用。比如 [NSThread currentThread]
,它可以获取当前线程信息,用于调试十分方便。而GCD
和NSOperation
都是系统自动管理线程周期。
GCD
是基于C
底层的API
,是一种更轻量级的,会自动合理地利用更多的CPU
内核(比如双核、四核)。以FIFO
(先进先出,后进后出)的顺序执行任务。GCD
中的核心概念:任务 和队列。任务:即操作,你想要干什么,说白了就是一段代码,在GCD
中就是一个 Block
,所以添加任务十分方便。任务有两种执行方式: 同步执行和异步执行,他们之间的区别是是否会创建新的线程。有串行、并发、主队列、全局队列、组队列。其中队列任务组合的方式总共有7种,其中使用频率最高的是:并发队列+异步执行(多个任务同时执行并发执行,会开启多条线程)。关于GCD
的一些其他具体组合使用方式,可查阅笔者之前的文章。点我查看
NSOperation
是基于GCD
更高一层的封装,相对于GCD
更加强大。可以给operation
之间添加依赖关系、取消一个正在执行的operation
、暂停和恢复operationQueue
等。关于NSOperation
的其他一些知识点,可查阅笔者之前的文章。点我查看
多线程的面试题目
- (1)A,B,C三个线程,要求执行完A,B后才能执行C,怎么做?
实现思路一: 添加依赖关系,A、B都依赖于C
//1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.创建操作
NSBlockOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"A----%@",[NSThread currentThread]);
}];
NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"B----%@",[NSThread currentThread]);
}];
NSBlockOperation *operationC = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"C----%@",[NSThread currentThread]);
}];
//3.添加依赖
[operationC addDependency:operationA]; // 让C 依赖于 A,则先执行A,再执行C
[operationC addDependency:operationB]; // 让C 依赖于 B,则先执行B,再执行C
//4.添加操作到队列中
[queue addOperation:operationA];
[queue addOperation:operationB];
[queue addOperation:operationC];
实现思路二:dispatch_group_notify
dispatch_queue_t queue = dispatch_queue_create("label", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"A----%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"B----%@",[NSThread currentThread]);
});
//等前面的任务执行完毕后 会自动执行这个任务
dispatch_group_notify(group, queue, ^{
NSLog(@"C----%@",[NSThread currentThread]);
});
实现思路三:dispatch_barrier_(a)sync
dispatch_queue_t queue = dispatch_queue_create("label", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"A----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"B----%@",[NSThread currentThread]);
});
// dispatch_barrier_async: 在它前面的任务执行结束后它才执行,在它后面的任务等它执行完成后才会执行
dispatch_barrier_async(queue, ^{
NSLog(@"C----%@",[NSThread currentThread]);
});
- (2)结合使用AFNetworking多次请求,实现线程同步以及依赖。
在使用AFNetworking之前,我们首先看一段代码
需求:吃饭 睡觉 打豆豆 -> 任务完成
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
NSLog(@"吃饭");
});
dispatch_group_async(group, queue, ^{
NSLog(@"睡觉");
});
dispatch_group_async(group, queue, ^{
NSLog(@"打豆豆");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"任务完成");
});
那么使用AFNetworking之后呢,会触发怎么的结果呢?我们一起拭目以待。
- (void)requestFormData{
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
[self requestWithType:@"吃饭"];
});
dispatch_group_async(group, queue, ^{
[self requestWithType:@"睡觉"];
});
dispatch_group_async(group, queue, ^{
[self requestWithType:@"打豆豆"];
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"任务完成");
});
}
- (void)requestWithType:(NSString *)type{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"http://www.mocky.io/v2/5b6685533200006a00ee11b1" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",type);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {}];
}
综上打印,你心里可能会产生一个疑惑,只是换成了AFNetworking,打印结果却发生了变化,用过AFNetworking的伙伴们应该都知道在进行网络请求的时候内部是又开了线程的,那么这时候我们应该怎么实现吃饭睡觉打豆豆这个需求呢,请接着继续往下看。
方式一:使用信号量进行破解
- (void)requestWithType:(NSString *)type{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"http://www.mocky.io/v2/5b6685533200006a00ee11b1" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
long flag = dispatch_semaphore_signal(semaphore);
NSLog(@"%@ --%ld",type,flag);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_semaphore_signal(semaphore);
}];
long flag = dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"%@ --%ld",type,flag);
}
方式二:dispatch_group_enter
和dispatch_group_leave
配合使用
- (void)requestFormData{
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
[self requestWithType:@"吃饭" group:group];
});
dispatch_group_async(group, queue, ^{
[self requestWithType:@"睡觉" group:group];
});
dispatch_group_async(group, queue, ^{
[self requestWithType:@"打豆豆" group:group];
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"任务完成");
});
}
- (void)requestWithType:(NSString *)type group:(dispatch_group_t)group{
//通知group,下面的任务马上要放到group中执行了
dispatch_group_enter(group);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"http://www.mocky.io/v2/5b6685533200006a00ee11b1" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//通知group,任务完成了,该任务要从group中移除了
dispatch_group_leave(group);
NSLog(@"%@",type);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_group_leave(group);
}];
}
如果上面对你来说太so seay
了,于是在此基础添加一个要求,吃完饭后,打会豆豆,之后便允许睡觉,才算完成任务了。革命尚未成功,同志仍需努力😁😁
使用AFNetworking
请求,实现线程同步以及依赖
- (void)requestFormData{
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
[self requestWithType:@"吃饭"];
}];
NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
[self requestWithType:@"睡觉"];
}];
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
[self requestWithType:@"打豆豆"];
}];
NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"任务完成");
}];
}];
[operation2 addDependency:operation1];
[operation4 addDependency:operation1];
[operation4 addDependency:operation2];
[operation4 addDependency:operation3];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperations:@[operation1,operation2, operation3, operation4] waitUntilFinished:NO];
}
- (void)requestWithType:(NSString *)type {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"http://www.mocky.io/v2/5b6685533200006a00ee11b1" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",type);
dispatch_semaphore_signal(semaphore);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
知识点补给站
-
addDependency
不能添加相互依赖,例如:A依赖B,B依赖A,这样会导致死锁
创建信号量,可以设置信号量的资源数。0表示没有资源。如:
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0)
等待信号,会让信号量值减一,当信号量值为0时会等待(直到超时),否则正常执行;如:
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
发送一个信号,会让信号总量加1。如:
dispatch_semaphore_signal(semaphore)
-
dispatch_group_enter
必须在dispatch_group_leave
之前出现 -
dispatch_group_enter
和dispatch_group_leave
必须成对出现 - 如果
dispatch_group_enter
比dispatch_group_leave
多一次,则wait函数等待的线程不会被唤醒和注册notify的回调block不会执行 - 如果
dispatch_group_leave
比dispatch_group_enter
多一次,则会引起崩溃。