IOS GCD基础(串 并)(同步 异步)个人理解


首先我们先通过程序来看 (串行队列,并行队列),(同步执行,异步执行) 的区别,大概就能明白这些名词的意思

1 串行队列+同步执行

//创建串行队列 DISPATCH_QUEUE_SERIAL

    dispatch_queue_t queue_serial = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);

    //同步执行

    dispatch_sync(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_serial, ^{

        sleep(2);

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

    });


先看懂输出的是什么。 前面xxxx-xx-xx (年月日) xx:xx:xx.xxxxxxx(时间)  后面的是nslog 输出的。(currentThread)是打印当前线程的详情

按照我们给的1-6按顺序输出了,  也就是先进先出 FIFO 。(好比我们排队打饭,先排进去的先打完饭出来)

大家都是number = 1 , 说明打饭给我们的人肯定是一个人。 


2 串行队列 + 异步执行

//串行队列 + 异步执行

    dispatch_queue_t queue_serial = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);

    //异步执行

    dispatch_async(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_async(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_async(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_async(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_async(queue_serial, ^{

        sleep(2);

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

    });

    dispatch_async(queue_serial, ^{

        sleep(2);

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

    });


同样按照1-6按照2秒间隔输出, 大家number = 4,说明也是同一个阿姨给大家打饭。但是为什么是另外一个阿姨了呢。(这就是异步执行的原因,因为食堂开辟了另外一个窗口,异步也就是开辟了另外一个通道,大家也都在这个通道排队打饭);



3 并行队列 + 同步执行

//并行队列,同步执行

    //并行队列

    dispatch_queue_t queue_concurrent = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);

    //同步执行

    dispatch_sync(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_sync(queue_concurrent, ^{

        sleep(2);

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

    });



number = 1, 因为是同步执行,开辟其他通道,也就是食堂没有其他打饭窗口,,大家并行队列的意思是,,,全部人没有排队 而是各种插队插队,,可是阿姨一次只能打一个人的饭,所以也还是谁先到窗口谁先打到饭。


4 并行队列 + 异步执行 

//并行队列,异步执行

    //并行队列

    dispatch_queue_t queue_concurrent = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);

    //同步执行

    dispatch_async(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_async(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_async(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_async(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_async(queue_concurrent, ^{

        sleep(2);

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

    });

    dispatch_async(queue_concurrent, ^{

        sleep(2);

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

    });



1-6没有按照顺序输出了,,number 也不只有一个了,因为食堂有其他打饭窗口,大家也都不像第二种情况,只在一个阿姨面前排队。所以大家也就不排队就各自找个窗口打饭了,



在总结下: 串行队列 是 大家排好队

 并行队列 是 大家不排队,各自有各自的列数


同步执行 是 只有一个通道 只有一个阿姨打饭

异步执行 是 新开辟了其他的通道 其他窗口也有阿姨打饭


组合一下 - 串行 + 同步  大家排好队+只在一个阿姨。每隔2s阿姨打完饭下一个

 串行 + 异步  大家排好队+虽然有很多个阿姨,但是约定了大家必须排队,所以大家老老实实每隔2s打完饭

 并行 + 同步  大家不排队+只有一个阿姨 但是一个阿姨的打饭速度决定只能2s执行完一个,

 并行 + 异步  大家不排队+有很多个阿姨 每个人都可以在2s钟后打完饭。同时打完


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值