iOS GCD中串行、并行、同步、异步执行顺序的研究

话不多说, 研究一点GCD的知识

1.串行队列+同步执行  DISPATCH_QUEUE_SERIAL为串行队列

- (void)gcdTest {

    dispatch_queue_t q = dispatch_queue_create("yzq.gsctest", DISPATCH_QUEUE_SERIAL);

    NSLog(@"S--%@",[NSThread currentThread]);

    dispatch_sync(q, ^{

        NSLog(@"sync--%@",[NSThread currentThread]);

    });

    NSLog(@"E--%@",[NSThread currentThread]);

}


首先我们来看一下打印的结果

2018-04-08 15:27:08.166974+0800 Test180403[2200:149142] S--<NSThread: 0x60000006a780>{number = 1, name = main}

2018-04-08 15:27:08.167221+0800 Test180403[2200:149142] sync--<NSThread: 0x60000006a780>{number = 1, name = main}

2018-04-08 15:27:08.167361+0800 Test180403[2200:149142] E--<NSThread: 0x60000006a780>{number = 1, name = main}

我们来分析一下:

我们创建了一个串行的一个队列,然后往这个队列中添加了一个同步的任务,首先在主线程中打印了S,此时我们添加的这个同步队列阻碍了当前的线程,需要等到这个线程中任务执行完毕才进行下一步的任务

2:串行队列+异步执行

- (void)gcdTest {

    dispatch_queue_t q = dispatch_queue_create("yzq.gsctest", DISPATCH_QUEUE_SERIAL);

    NSLog(@"S--%@",[NSThread currentThread]);

    dispatch_async(q, ^{

        NSLog(@"sync--%@",[NSThread currentThread]);

    });

    NSLog(@"E--%@",[NSThread currentThread]);

}

我们来看打印结果:

2018-04-08 15:38:55.435107+0800 Test180403[2340:155472] S--<NSThread: 0x604000076b00>{number = 1, name = main}

2018-04-08 15:38:55.435392+0800 Test180403[2340:155472] E--<NSThread: 0x604000076b00>{number = 1, name = main}

2018-04-08 15:38:55.435412+0800 Test180403[2340:155537] sync--<NSThread: 0x604000462380>{number = 3, name = (null)}


分析:

我们创建了一个串行的一个队列,首先在主线程中打印了S,然后往这个队列中添加了一个异步的任务,但是此时也只是添加进去了并没有立即执行这个线程,当主线程的任务执行完毕了才会取出主线程中的异步队列进行执行.

3.并行队列+同步执行 DISPATCH_QUEUE_CONCURRENT为并行队列

- (void)gcdTest {

    dispatch_queue_t q = dispatch_queue_create("yzq.gsctest", DISPATCH_QUEUE_CONCURRENT);

    NSLog(@"S--%@",[NSThread currentThread]);

    dispatch_sync(q, ^{

        NSLog(@"sync--%@",[NSThread currentThread]);

    });

    NSLog(@"E--%@",[NSThread currentThread]);

}


看看打印结果:

2018-04-08 15:46:10.614840+0800 Test180403[2432:159361] S--<NSThread: 0x60400006da40>{number = 1, name = main}

2018-04-08 15:46:10.615052+0800 Test180403[2432:159361] sync--<NSThread: 0x60400006da40>{number = 1, name = main}

2018-04-08 15:46:10.615169+0800 Test180403[2432:159361] E--<NSThread: 0x60400006da40>{number = 1, name = main}


分析:

我们创建了一个并行的队列,刚开始会在主线程中打印S 然后往这个并行队列中添加了一个同步任务,此时主线程在执行dispatch_sync方法后阻塞了当前线程,去等待dispatch_sync方法里面的block执行完毕,等到所有同步任务执行完毕后,才去执行后面的操作,所有的打印都是在主线程完成的。

4:并行队列+异步执行

- (void)gcdTest {

    dispatch_queue_t q = dispatch_queue_create("yzq.gsctest", DISPATCH_QUEUE_CONCURRENT);

    NSLog(@"S--%@",[NSThread currentThread]);

    dispatch_async(q, ^{

        NSLog(@"async--%@",[NSThread currentThread]);

    });

    NSLog(@"E--%@",[NSThread currentThread]);

}


看看打印结果:

2018-04-08 15:52:59.861249+0800 Test180403[2503:162803] S--<NSThread: 0x600000078180>{number = 1, name = main}

2018-04-08 15:52:59.861584+0800 Test180403[2503:162803] E--<NSThread: 0x600000078180>{number = 1, name = main}

2018-04-08 15:52:59.861603+0800 Test180403[2503:162867] async--<NSThread: 0x604000279340>{number = 3, name = (null)}


分析一下:

首先创建了一个并行的队列,打印S,然后往这个并行队列中添加了一个异步线程,但是并没有立即执行,当主线程任务执行完毕之后再从并行队列中依次开辟线程来执行操作.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值