Objective-c并发编程 -- GCD代码演示

并发队列(全局队列) + 同步任务

没有开启新的线程, 任务是逐个执行的

代码

    // 获取全局队列
    // dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 创建并发队列
    dispatch_queue_t queue = dispatch_queue_create("myQueur", DISPATCH_QUEUE_CONCURRENT);
    
    // 在队列里添加任务
    // 同步任务
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

输出

GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----1------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----2------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}
GCDDemo[19855:1999638] ----3------<NSThread: 0x608000069700>{number = 1, name = main}

并发队列(全局队列) + 异步任务

开启了新线程, 任务是并发的

代码

    // 获取全局队列
    // dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 创建并发队列
    dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_CONCURRENT);
    
    // 异步任务
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

输出

GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}
GCDDemo[22092:2084969] ----3------<NSThread: 0x610000263380>{number = 5, name = (null)}
GCDDemo[22092:2084991] ----1------<NSThread: 0x61800007f940>{number = 3, name = (null)}
GCDDemo[22092:2084970] ----2------<NSThread: 0x60800007db80>{number = 4, name = (null)}

串行队列 + 同步任务

没有开启新的线程, 任务是逐个执行的

代码

    // 创建并发队列
    dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL);
    
    // 同步任务
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

输出

GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----1------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----2------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}
GCDDemo[23292:2148098] ----3------<NSThread: 0x618000065700>{number = 1, name = main}

串行队列 + 异步任务

开启新的线程, 任务是逐个执行的

代码

    // 创建串行队列
    dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL);
    
    // 异步任务
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

输出

GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----1------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----2------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}
GCDDemo[23834:2175316] ----3------<NSThread: 0x600000267740>{number = 3, name = (null)}

主队列 + 同步任务

会造成死锁的线程【不能在主队列里添加同步任务】

代码

    // 获得主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 同步任务
    dispatch_sync(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });

主队列 + 异步任务

没有开启新的线程, 任务是逐个执行的

代码

        // 获得主队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    // 异步任务
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----1------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----2------%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            NSLog(@"----3------%@", [NSThread currentThread]);
        }
    });

输出

GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----1------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----2------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
GCDDemo[25004:2229546] ----3------<NSThread: 0x618000075200>{number = 1, name = main}
    原文作者:移动开发
    原文地址: https://my.oschina.net/jasper1987/blog/831023
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞