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

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


ios 主队列里使用串行队列 ios异步串行队列_串行队列

先看懂输出的是什么。 前面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]);
    });


ios 主队列里使用串行队列 ios异步串行队列_ios 主队列里使用串行队列_02

同样按照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]);
    });

ios 主队列里使用串行队列 ios异步串行队列_先进先出_03


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


ios 主队列里使用串行队列 ios异步串行队列_先进先出_04

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

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

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

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

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