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

  • 相关阅读:
    爬虫系列---多线程爬取实例
    爬虫系列---selenium详解
    爬虫系列二(数据清洗--->bs4解析数据)
    爬虫系列二(数据清洗--->xpath解析数据)
    爬虫系列二(数据清洗--->正则表达式)
    爬虫实例系列一(requests)
    selenium PO模式
    setUp和tearDown及setUpClass和tearDownClass的用法及区别
    chromeIEFirefox驱动下载地址
    HTTP通信机制
  • 原文地址:https://www.cnblogs.com/zhou--fei/p/6183586.html
Copyright © 2011-2022 走看看