zoukankan      html  css  js  c++  java
  • GCD网络多线程---同步运行,异步运行,串行队列,并行队列

        总结:同步(无论是串行还是并行)----不又一次开辟子线程

            异步(无论是串行还是并行)----开辟子线程

        

        

        GCD:

            dispatch queue

           主线程的main queue

           并行队列 global dispatch queue

           串行队列serial queues 一般用于按顺序同步訪问



    #pragma mark - 载入多线程

    - (void) _loadMutil

    {

        

        //GCD基于C语言


        //1.主对列:(串行队列)

        dispatch_queue_t mainQueue=dispatch_get_main_queue();

        

        

        //2.全局并行队列

        dispatch_queue_t concu=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

        

        

        //3.创建串行队列

       dispatch_queue_t queueSerial=dispatch_queue_create("jrqueue1",DISPATCH_QUEUE_SERIAL);

        

        

        //4.创建并行队列

        dispatch_queue_t queueConcu=dispatch_queue_create("jrqueue2",DISPATCH_QUEUE_CONCURRENT);

        

        

        //同步运行+串行队列

       /*

         dispatch_sync(queueSerial, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"同步串行队列1-----%@",[NSThread currentThread]);

         });

         

         dispatch_sync(queueSerial, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"同步串行队列2-----%@",[NSThread currentThread]);

         });

         */

        

        //同步运行+并行队列

       /*

         dispatch_sync(queueConcu, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"同步并行队列1-----%@",[NSThread currentThread]);

         });

         

         dispatch_sync(queueConcu, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"同步并行队列2-----%@",[NSThread currentThread]);

         });

         */

        

        //异步运行+串行队列-----开启一个子线程,且顺序运行

       /*

         dispatch_async(queueSerial, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"异步串行队列1-----%@",[NSThread currentThread]);

         });

         

         dispatch_async(queueSerial, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"异步串行队列2-----%@",[NSThread currentThread]);

         });

         

         dispatch_async(queueSerial, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"异步串行队列3-----%@",[NSThread currentThread]);

         });

         */

        

        //异步运行+并行队列----开启多个线程,且并发运行(无序)

       /*

         dispatch_async(queueConcu, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"异步并行队列1-----%@",[NSThread currentThread]);

         });

         

         dispatch_async(queueConcu, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"异步并行队列2-----%@",[NSThread currentThread]);

         });

         

         dispatch_async(queueConcu, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"异步并行队列3-----%@",[NSThread currentThread]);

         });

         */

        

        //主对列+同步运行-----死锁(将下面两个加入到主队列,等待前面的运行完毕(loadView

    loadData之类的),可是当运行到这一步时,形成死循环)

       /*

         dispatch_sync(mainQueue, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"同步主队列1-----%@",[NSThread currentThread]);

         });

         

         dispatch_sync(mainQueue, ^{

         [NSThread sleepForTimeInterval:2];

         NSLog(@"同步主队列2-----%@",[NSThread currentThread]);

         });

         */


    }

    @end


    将以上的方法,在viewDidLoad 中调用一下。看看效果怎样~



  • 相关阅读:
    在try{}里面有一个return语句,那么紧跟在后面的finally{}里面的code还会执行吗?
    编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中
    将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad
    flex实现图表事件获取数据
    让Powerdesigner15支持C#3.5的自动属性(一)
    安装Windows2008后,IIS中网站遇到的问题及解决方法
    让Powerdesigner15支持C#3.5的自动属性(二)
    我与一个女程序员的聊天记录一
    你是我的玫瑰类关系阐微
    制作Winform的ToolBox控件
  • 原文地址:https://www.cnblogs.com/llguanli/p/7356907.html
Copyright © 2011-2022 走看看