当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS 控制任务执行顺序

iOS 控制任务执行顺序

2018年10月22日  | 移动技术网IT编程  | 我要评论

csol自雷脚本,蒙牛酸奶官网,斗战神监察特使

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 {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 {number = 3, name = (null)}
2017-07-18 16:42:58.872 test[4336:640550] awake up222222

^^^^
重点 你可以观察到 先打印了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 被设置成等待, 那可能需要等待.

结合最后一条, 所以我们现在来使用依赖实现我们的需求.

 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];

1.4 方法四, 既然nsoperation是对gcd的封装,那么联想方法一, 我想能不能将nsoperationqueue也设置成串行队列.
但是nsoperationqueue好像屏蔽了这个串行并行这个概念, 而是使用 queue.maxconcurrentoperationcount 来设置queue一次能够执行的任务量.
如果设置成1. 就类似于串行队列, 任务会 顺序执行 . 但是只是类似, 因为在串行队列 + 异步执行时,会开辟一条子线程. 但是将maxconcurrentoperationcount 设置成1后, 有可能会开辟多条线程,也就是说, 任务一和任务二可能会在同一线程上执行, 也可能在不同线程上执行.

  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")

打印结果

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 {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 {number = 4, name = (null)}
2017-07-18 19:14:03.110 test[4691:708481] awake up222222

观察可知, 当前两个任务没有在同一线程上执行,但是sleep222 和awake up222都是在 awake111后执行的, 也即是说是顺序执行。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网