OC笔记——GCD队列的简单使用

图4


图3


图2


图1


// 1. 队列的基本类型及创建

    //串行队列的创建

    dispatch_queue_t my_serial_queue;

    my_serial_queue = dispatch_queue_create("com.serial.queue", NULL);

   //并发队列,采用默认优先级

    dispatch_queue_t myQueue;

    myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    //#define DISPATCH_QUEUE_PRIORITY_HIGH 2

    //#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0

  //#define DISPATCH_QUEUE_PRIORITY_LOW (-2)

  //#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN


    //主线程队列

    dispatch_queue_t main_queue = dispatch_get_main_queue();

   //获取当前队列..  已经弃用

    dispatch_queue_t current_queue = dispatch_get_current_queue();

    //生成串行队列

    dispatch_queue_t serial_queue =  dispatch_queue_create("com.serial.queue", DISPATCH_QUEUE_SERIAL);

   dispatch_queue_t queue1 = dispatch_queue_create("com.serial.queue", NULL);

   //生成并发队列 2种方式

   dispatch_queue_t concurrent_queue2 = dispatch_queue_create("com.concurretnt_queue", DISPATCH_QUEUE_CONCURRENT);

    //只能生成并发队列

    dispatch_queue_t queue4 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

   /*下面是一些队列类型的创建 ,我们打印出来1。2。3。4。的顺序,看队列的具体执行结果

      同步 异步 强调的是线程,同步为当前线程中,异步为在多个新开辟的子线程中执行,

    串行队列 并行队列 强调的是执行顺序,串行为一个接一个的顺序执行,(按程序的正常顺序走。。。)当前运行的任务只有一个; 而并行则为同时并发,没有确定的执行顺序,当前运行的任务可能有多个 */

    /*我们可以把同步异步想象为交通的几个车道,同步为左转车道。。异步为左转车道+直行车道+右转车道

      串行。并行我们想象为 车道上的车辆,这样就容易理解些了,先考虑同步异步问题(看自己在哪条路上),再考虑串行并行(自身的道路上的排列顺序),就知道任务的执行顺序了

    */

    // 2. 创建串行同步队列

    dispatch_queue_t queue = dispatch_queue_create("com.serial.queue", NULL);

    dispatch_sync(queue, ^{

        //设置同步队列,参数为执行队列的名称,执行语句

        [self printThread:1];

    });

    dispatch_sync(queue, ^{

        [self printThread:2];

    });

    dispatch_sync(queue, ^{

        [self printThread:3];

    });

    dispatch_sync(queue, ^{

        [self printThread:4];

    });

  结果如图1

    //串行同步队列中,任务都在当前线程执行(同步),并且顺序执行(串行)

    //  2. 串行异步执行

    dispatch_async(queue, ^{

        [self printThread:1];

    });

    dispatch_async(queue, ^{

        [self printThread:2];

    });

    dispatch_async(queue, ^{

        [self printThread:3];

    });

    dispatch_async(queue, ^{

        [self printThread: 4];

    });

结果为图2

    //在串行异步队列中,任务都在新开辟的子线程中执行(异步),并不是主线程了,并且顺序执行(串行)

   // 3. 并发同步队列

    dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_sync(queue2, ^{

        [self printThread:1];

    });

    dispatch_sync(queue2, ^{

        [self printThread:2];

    });

    dispatch_sync(queue2, ^{

        [self printThread:3];

    });

    dispatch_async(queue2, ^{

        [self printThread:4];

    });

   结果为图3

    //在并发同步队列中,线程为当前线程中(同步),只有一条线程,会顺序执行,并发的特别并没有体现出来,同步会阻塞当前线程


    // 4. 并发异步队列

    dispatch_async(queue2, ^{

        [self printThread:1];

    });

    dispatch_async(queue2, ^{

        [self printThread:2];

    });

    dispatch_async(queue2, ^{

        [self printThread:3];

    });

    dispatch_async(queue2, ^{

        [self printThread:4];

    });

    结果为图4

    //再并发异步中,先考虑异步,再新开辟的子线程中,并且并发执行,任务同时进行,没有顺序可言

   //平时我们使用使用最多的是并发异步队列,比如开辟多个子线程下载图片/文件等

你可能感兴趣的:(OC笔记——GCD队列的简单使用)