zoukankan      html  css  js  c++  java
  • GCD中的dispatch_sync、dispatch_sync 分别与串行、并行队列组合执行小实验

    平常开发中会经常用gcd做一下多线程任务,但一直没有对同步、异步任务在串行、并行队列的执行情况做个全面的认识,今天写了个demo跑了下,还是有些新发现的。

    代码如下:

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

        [self gcdTest];

    }

    -(void)gcdTest

    {

        dispatch_queue_t serialQueue= dispatch_queue_create("串行队列", DISPATCH_QUEUE_SERIAL);

        dispatch_queue_t concurrentQueue=dispatch_queue_create("并行队列", DISPATCH_QUEUE_CONCURRENT);

        

        dispatch_queue_t mainQueue=dispatch_get_main_queue();

        dispatch_queue_t globalQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        

        //1:

    //    for(int i=0;i<10;i++){

    //        dispatch_sync(serialQueue, ^{

    //            NSLog(@"任务执行中....-%d mainThread:%@",i,[NSThread currentThread]);

    //        });

    //    }

        //结果,同步任务在串行队列中,在主线程下串行执行

        

        //2:

    //    for(int i=0;i<10;i++){

    //        dispatch_sync(concurrentQueue, ^{

    //            NSLog(@"任务执行中....-%d mainThread:%@",i,[NSThread currentThread]);

    //        });

    //    }

        //结果,同步任务在并行队列中,在主线程下串行执行

        

        //3:

    //    for(int i=0;i<10;i++){

    //        dispatch_sync(mainQueue, ^{

    //            NSLog(@"任务执行中....-%d mainThread:%@",i,[NSThread currentThread]);

    //        });

    //    }

        //结果,同步任务在主队列中,锁死

        

        //4:

    //    for(int i=0;i<10;i++){

    //        dispatch_sync(globalQueue, ^{

    //            NSLog(@"任务执行中....-%d mainThread:%@",i,[NSThread currentThread]);

    //        });

    //    }

        //结果,同步任务在全局队列中,在主线程下串行执行

        

        //5:

    //    for(int i=0;i<10;i++){

    //        dispatch_async(serialQueue, ^{

    //            NSLog(@"任务执行中....-%d mainThread:%@",i,[NSThread currentThread]);

    //        });

    //    }

        //结果,异步任务在串行队列中,在一个子线程中串行执行

        

        //6:

    //    for(int i=0;i<10;i++){

    //        dispatch_async(concurrentQueue, ^{

    //            NSLog(@"任务执行中....-%d mainThread:%@",i,[NSThread currentThread]);

    //        });

    //    }

        //结果,异步任务在并行队列中,在多个子线程中并行执行

        

        //7:

    //    for(int i=0;i<10;i++){

    //        dispatch_async(mainQueue, ^{

    //            NSLog(@"任务执行中....-%d mainThread:%@",i,[NSThread currentThread]);

    //        });

    //    }

        //结果,异步任务在主队列中,在主线程中串行执行

        

        //8:

        for(int i=0;i<10;i++){

            dispatch_async(globalQueue, ^{

                NSLog(@"任务执行中....-%d mainThread:%@",i,[NSThread currentThread]);

            });

        }

        //结果,异步任务在全局队列中,在多个子线程中并行执行

    }

    总结如下:

    dispatch_sync:同步任务无论在自定义串行队列、自定义并行队列、主队列(当前线程为主线程时会出现死锁)、全局队列 执行任务时,都不会创建子线程,而是在当前线程中串行执行;

    dispatch_async:异步任务无论在自定义串行队列、自定义并行队列(主队列除外,主队列下,任务会在主线中串行执行)、全局队列 执行任务时,都会创建子线程,并且在子线程中执行;

  • 相关阅读:
    在Rancher中添加为中国区优化的k8s应用商店的步骤和方法
    debian源配置实例
    面向对象(六)——元类
    异常处理
    面向对象(五)——isinstance与issubclass、反射、内置方法
    面向对象(四)——classmethod、staticmethod装饰器(绑定方法与非绑定方法)
    面向对象(三)——组合、多态、封装、property装饰器
    面向对象(二)——继承
    面向对象(一)——类与对象介绍
    ATM+购物车结构
  • 原文地址:https://www.cnblogs.com/zhou--fei/p/6183586.html
Copyright © 2011-2022 走看看