摘要:本文将带你了解IOS开发入门iOS 控制任务执行顺序,希望本文对大家学IOS有所帮助。
本文将带你了解IOS开发入门iOS 控制任务执行顺序,希望本文对大家学IOS有所帮助。
iOS 控制任务执行顺序,需求:
有两个任务都要放在子线程中执行, 且要保证A执行完再执行B.
如果你也有同样的需求, 可以从下面几种方法中挑选一种
实现方法
1.1. 方法一 使用GCD串行队列. 先创建一个串行队列, 将任务加入到串行队列中, 因为是串行队列,能保证顺序执行, 且因为是串行队列,所以两个任务是在同一线程执行.
dispatch_queue_t queue = dispatch_queue_create("myCostom", DISPATCH_QUEUE_SERIAL);
NSLog(@"dispatch111 start");
dispatch_async(queue, ^{
NSLog(@"sleep1111");
NSLog(@"dispatch111 %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:5];
NSLog(@"awake up111");
});
NSLog(@"dispatch2222 start");
dispatch_async(queue, ^{
NSLog(@"sleep 2222");
NSLog(@"dispatch2222 %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:5];
NSLog(@"awake up222222");
});
NSLog(@"dispatch 33333");
1.2 方法二 使用barrier.
dispatch_queue_t queue = dispatch_queue_create("mycustom", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"dispatch111 start");
dispatch_async(queue, ^{
NSLog(@"sleep1111");
NSLog(@"dispatch111 %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:5];
NSLog(@"awake up111");
});
dispatch_barrier_async(queue, ^{
NSLog(@"------------------- barrier");
});
NSLog(@"dispatch2222 start");
dispatch_async(queue, ^{
NSLog(@"sleep 2222");
NSLog(@"dispatch2222 %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:5];
NSLog(@"awake up222222");
});
NSLog(@"dispatch 33333");
打印结果
2017-07-18 16:42:48.866 Test[4336:640379] dispatch111 start
2017-07-18 16:42:48.866 Test[4336:640379] dispatch2222 start
2017-07-18 16:42:48.866 Test[4336:640550] sleep1111
2017-07-18 16:42:48.866 Test[4336:640379] dispatch 33333
2017-07-18 16:42:48.866 Test[4336:640550] dispatch111 <nsthread: 0x60000026f700="">{number = 3, name = (null)}
2017-07-18 16:42:53.869 Test[4336:640550] awake up111
2017-07-18 16:42:53.870 Test[4336:640550] ------------------- barrier
2017-07-18 16:42:53.870 Test[4336:640550] sleep 2222
2017-07-18 16:42:53.870 Test[4336:640550] dispatch2222 <nsthread: 0x60000026f700="">{number = 3, name = (null)}
2017-07-18 16:42:58.872 Test[4336:640550] awake up222222</nsthread:></nsthread:>
^^^^
重点 你可以观察到 先打印了dispatch 333 在打印 barrier, 说明barrier 阻塞的不是barrier所在的线程,而是queue所在的线程
重点如果我把 dispatch_barrier_async 换成 dispatch_barrier_sync 这就连当前线程也阻塞了
1.3 方法三 使用NSOperation
在这之前简单介绍一下 NSOperation
* NSOperation 是对GCD的封装.
* NSOperation 是一个抽象类, 不能封装任务, 需要他的两个子类完成 NSInvocationOperation 和 NSBlockOperation
* 需要调用start 方法开启, 默认是同步执行,也就是说默认不会开线程, 也可以使用 cancel 方法取消, 但是只能取消未开始的任务,已经开始的任务,无法被取消.
* 在NSOperation 添加到queue后, 通常会在很短的时间内得到运行, 但是如果Operation有添加依赖或者queue 被设置成等待, 那可能需要等待.
结合最后一条, 所以我们现在来使用依赖实现我们的需求.
<code><code><code><code> NSOperationQueue * queue = [[NSOperationQueue alloc] init];
NSBlockOperation * op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"sleep1111");
NSLog(@"dispatch111 %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:5];
NSLog(@"awake up111");
}];
NSBlockOperation * op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"sleep 2222");
NSLog(@"dispatch2222 %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:5];
NSLog(@"awake up222222");
}];
[op1 addDependency:op2];
[queue addOperation:op1];
[queue addOperation:op2];</code></code></code></code>
1.4 方法四, 既然NSOperation是对GCD的封装,那么联想方法一, 我想能不能将NSOperationQueue也设置成串行队列.
但是NSOperationQueue好像屏蔽了这个串行并行这个概念, 而是使用 queue.maxConcurrentOperationCount 来设置queue一次能够执行的任务量.
如果设置成1. 就类似于串行队列, 任务会 顺序执行 . 但是只是类似, 因为在串行队列 + 异步执行时,会开辟一条子线程. 但是将maxConcurrentOperationCount 设置成1后, 有可能会开辟多条线程,也就是说, 任务一和任务二可能会在同一线程上执行, 也可能在不同线程上执行.
<code><code><code><code><code> NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
NSLog(@"NSOperation111 start");
NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"sleep1111");
NSLog(@"NSOperation111 %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:3];
NSLog(@"awake up111");
}];
NSLog(@"NSOperation2222 start");
NSInvocationOperation *operationI = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testInvocaionOperatation) object:nil];
[queue addOperation:operationB];
[queue addOperation:operationI];
NSLog(@"NSOperation 33333")</code></code></code></code></code>
打印结果
<code><code><code><code><code>2017-07-18 19:13:56.971 Test[4691:708449] NSOperation111 start
2017-07-18 19:13:56.972 Test[4691:708449] NSOperation2222 start
2017-07-18 19:13:56.972 Test[4691:708449] NSOperation 33333
2017-07-18 19:13:56.972 Test[4691:708483] sleep1111
2017-07-18 19:13:56.973 Test[4691:708483] NSOperation111 <nsthread: 0x60800026e200="">{number = 3, name = (null)}
2017-07-18 19:14:00.048 Test[4691:708483] awake up111
2017-07-18 19:14:00.049 Test[4691:708481] sleep 2222
2017-07-18 19:14:00.049 Test[4691:708481] NSOperation2222 <nsthread: 0x60800026e800=
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号