美文网首页
小驴拉磨之iOS进阶-多线程(7)—— GCD 同步任务

小驴拉磨之iOS进阶-多线程(7)—— GCD 同步任务

作者: 小驴拉磨 | 来源:发表于2017-11-16 15:07 被阅读6次
    11111.jpg

    同步任务

    在开发中,通常会将耗时操作放后台执行,有的时候,有些任务彼此有依赖关系!
    例子:登录、支付、下载
    在队列里,依赖执行同一时间。

    -(void)GCDSerialQueuesSixe {
        //创建一个异步队列
        dispatch_queue_t q = dispatch_queue_create("cc_queue2",DISPATCH_QUEUE_CONCURRENT);
        //1.用户登录
        dispatch_sync(q,^{
            NSLog(@"用户登录 %@",[NSThread currentThread]);
            [NSThread sleepForTimeInterval:5.0];
        });
        
        //2.支付
        dispatch_async(q,^{
            NSLog(@"支付 %@",[NSThread currentThread]);
        });
        
        //3.下载
        dispatch_async(q,^{
            NSLog(@"下载  %@",[NSThread currentThread]);
        });
    }
    
    • 执行结果


      WX20171116-145320.png
    • 步骤

    新建一个队列,并加入3个任务
    设置登录任务为dispatch_sync 同步任务。这个任务必须先执行,它执行的时候,因为是同步,不会去GCD拿线程。等登录任务完成后,在执行支付任务和下载任务时,会用子线程去做!
    同步任务 没有执行完毕,后面的所有任务都不会去执行。所以它相当于一个🔐的功能。
    利用同步任务,能够做到任务依赖关系。前一个任务是同步任务,它不执行完成,队列就不会调度后面的任务。

    增强版同步任务

    -(void)GCDSerialQueuesSeven
    {
        //队列
        dispatch_queue_t q = dispatch_queue_create("cc_queue",DISPATCH_QUEUE_CONCURRENT);
        
        //任务,在这个任务中添加了3个任务
        void (^task)() = ^{
            //1.用户登录
            dispatch_async(q,^{
                NSLog(@"用户登录 %@",[NSThread currentThread]);
            });
            //2.支付
            dispatch_async(q,^{
                NSLog(@"支付 %@",[NSThread currentThread]);
            });
            //3.下载
            dispatch_async(q,^{
                NSLog(@"下载  %@",[NSThread currentThread]);
            });
        };
        for(int i = 0; i < 10; I++)
        {
            NSLog(@"%d %@",i,[NSThread currentThread]);
        }
        //将task 丢到异步执行中去。
        dispatch_async(q,task);
        NSLog(@"come here");
    }
    
    • 打印结果

    2017-11-16 14:59:56.321231+0800 GCDDome-串行队列[4335:2580612] 0 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321298+0800 GCDDome-串行队列[4335:2580612] 1 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321327+0800 GCDDome-串行队列[4335:2580612] 2 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321352+0800 GCDDome-串行队列[4335:2580612] 3 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321372+0800 GCDDome-串行队列[4335:2580612] 4 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321397+0800 GCDDome-串行队列[4335:2580612] 5 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321441+0800 GCDDome-串行队列[4335:2580612] 6 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321484+0800 GCDDome-串行队列[4335:2580612] 7 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321508+0800 GCDDome-串行队列[4335:2580612] 8 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321529+0800 GCDDome-串行队列[4335:2580612] 9 <NSThread: 0x1c407c980>{number = 1, name = main}
    2017-11-16 14:59:56.321552+0800 GCDDome-串行队列[4335:2580612] come here
    2017-11-16 14:59:56.322581+0800 GCDDome-串行队列[4335:2580659] 用户登录 <NSThread: 0x1c446d5c0>{number = 3, name = (null)}
    2017-11-16 14:59:56.323269+0800 GCDDome-串行队列[4335:2580659] 下载 <NSThread: 0x1c446d5c0>{number = 3, name = (null)}
    2017-11-16 14:59:56.323306+0800 GCDDome-串行队列[4335:2580662] 支付 <NSThread: 0x1c02721c0>{number = 4, name = (null)}

    • 代码分析:
      首先async异步,会开启一个子线程,执行任务task;
      任务task,包含了3个子任务;
      将任务添加到队列中,完成后task任务完成;
      队列此时添加了3个任务;
      先在子线程上执行同步登录任务;等待完成后;
      将支付异步任务在子线程上执行,等到执行到1半,将下载异步任务重新开辟一条线程执行。直到全部结束!

    • 结论
      登录、下载、支付都在子线程
      先执行“登录”,支付和下载不确定先后;
      come here立马执行!靠前。因为come here在主线程
      for 循环执行位置与添加任务同级别。task会顺序执行。

    相关文章

      网友评论

          本文标题:小驴拉磨之iOS进阶-多线程(7)—— GCD 同步任务

          本文链接:https://www.haomeiwen.com/subject/wqvlvxtx.html