OC 线程操作 - GCD队列组

Posted qingzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OC 线程操作 - GCD队列组相关的知识,希望对你有一定的参考价值。

队列组两种使用方

/** 新方法
 队列组一般用在在异步操作,在主线程写队列组毫无任何作用
 */
- (void)GCD_Group_new_group___notify{
    dispatch_queue_t queue = dispatch_queue_create("11", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t globalqueue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    /*
     1.封装任务
     2.把任务加到队列
     3.会监听任务的执行情况
     */
    dispatch_group_async(group, globalqueue, ^{
        NSLog(@"1111---%@---", [NSThread currentThread]);
    });
    
    dispatch_group_async(group, globalqueue, ^{
        NSLog(@"22222---%@---", [NSThread currentThread]);
    });
    
    dispatch_group_async(group, globalqueue, ^{
        NSLog(@"333---%@---", [NSThread currentThread]);
    });
    
    // 一定要加上这行,不然不起任何作用
    dispatch_group_notify(group, globalqueue, ^{
        NSLog(@"完成----4444---%@---", [NSThread currentThread]);
    });
    
    /*
     打印结果:
     2018-06-28 10:18:44.191407+0800 5线程操作-GCD-快速迭代[7808:56262] 333---<NSThread: 0x6000002682c0>{number = 4, name = (null)}---
     2018-06-28 10:18:44.191409+0800 5线程操作-GCD-快速迭代[7808:56266] 1111---<NSThread: 0x608000075300>{number = 3, name = (null)}---
     2018-06-28 10:18:44.191434+0800 5线程操作-GCD-快速迭代[7808:56264] 22222---<NSThread: 0x600000266580>{number = 5, name = (null)}---
     2018-06-28 10:18:44.191758+0800 5线程操作-GCD-快速迭代[7808:56264] 完成----4444---<NSThread: 0x600000266580>{number = 5, name = (null)}---
     */
}

 

//老式写法
- (void)GCD_GroupDemo_old___enter_leave{
    dispatch_queue_t queue = dispatch_queue_create("11", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t globalqueue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    
    
    // 旧 写法
    dispatch_group_async(group, globalqueue, ^{
        NSLog(@"离开---%@---", [NSThread currentThread]);
        dispatch_group_leave(group);
    });
    dispatch_group_async(group, globalqueue, ^{
        NSLog(@"222---%@---", [NSThread currentThread]);
    });
    dispatch_group_async(group, globalqueue, ^{
        NSLog(@"333---%@---", [NSThread currentThread]);
    });
    
    dispatch_group_async(group, globalqueue, ^{
        NSLog(@"111---%@---", [NSThread currentThread]);
    });
    /*
     2018-06-28 10:25:06.997243+0800 5线程操作-GCD-快速迭代[7942:62493] 离开---<NSThread: 0x60c00007f8c0>{number = 3, name = (null)}---
     2018-06-28 10:25:06.997286+0800 5线程操作-GCD-快速迭代[7942:62491] 333---<NSThread: 0x60800007d240>{number = 5, name = (null)}---
     2018-06-28 10:25:06.997306+0800 5线程操作-GCD-快速迭代[7942:62489] 111---<NSThread: 0x608000260c80>{number = 6, name = (null)}---
     2018-06-28 10:25:06.997287+0800 5线程操作-GCD-快速迭代[7942:62492] 222---<NSThread: 0x60c00007de40>{number = 4, name = (null)}---
     */
}


以上是关于OC 线程操作 - GCD队列组的主要内容,如果未能解决你的问题,请参考以下文章

[OC学习笔记]多线程之GCD

[OC学习笔记]多线程之GCD

[OC学习笔记]浅学GCD线程相关内容

GCD线程依赖,GCD队列组,线程监听,线程等待

OC 线程操作 - GCD快速迭代

第二天-多线程