随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

1.队列组两种使用方法
2.队列组等待 wait

复制代码
复制代码
/** 新方法
 队列组一般用在在异步操作,在主线程写队列组毫无任何作用
 */
- (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)}---
     */


复制代码
    //DISPATCH_TIME_FOREVER :等待队列所有任务执行完后 执行下面代码后面的内容
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    NSLog(@"执行完了");
    /*
     打印结果:
     2018-06-28 10:58:34.631990+0800 5线程操作-GCD-快速迭代[8500:86081] 1111---<NSThread: 0x60400007e180>{number = 3, name = (null)}---
     2018-06-28 10:58:34.632041+0800 5线程操作-GCD-快速迭代[8500:86084] 22222---<NSThread: 0x60400007e4c0>{number = 4, name = (null)}---
     2018-06-28 10:58:34.632065+0800 5线程操作-GCD-快速迭代[8500:86080] 333---<NSThread: 0x60400007e380>{number = 5, name = (null)}---
     2018-06-28 10:58:34.633261+0800 5线程操作-GCD-快速迭代[8500:86052] 执行完了
     2018-06-28 10:58:34.633284+0800 5线程操作-GCD-快速迭代[8500:86080] 完成----4444---<NSThread: 0x60400007e380>{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)}---
     */
}

复制代码
posted on   懂事长qingzZ  阅读(479)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示