zoukankan      html  css  js  c++  java
  • 刀哥多线程之调度组gcd-12-group

    调度组

    常规用法

    - (void)group1 {
    
        // 1. 调度组
        dispatch_group_t group = dispatch_group_create();
    
        // 2. 队列
        dispatch_queue_t q = dispatch_get_global_queue(0, 0);
    
        // 3. 将任务添加到队列和调度组
        dispatch_group_async(group, q, ^{
            [NSThread sleepForTimeInterval:1.0];
            NSLog(@"任务 1 %@", [NSThread currentThread]);
        });
        dispatch_group_async(group, q, ^{
            NSLog(@"任务 2 %@", [NSThread currentThread]);
        });
        dispatch_group_async(group, q, ^{
            NSLog(@"任务 3 %@", [NSThread currentThread]);
        });
    
        // 4. 监听所有任务完成
        dispatch_group_notify(group, q, ^{
            NSLog(@"OVER %@", [NSThread currentThread]);
        });
    
        // 5. 判断异步
        NSLog(@"come here");
    }

    enter & leavel

    // MARK: - 调度组 2
    - (void)group2 {
        // 1. 调度组
        dispatch_group_t group = dispatch_group_create();
    
        // 2. 队列
        dispatch_queue_t q = dispatch_get_global_queue(0, 0);
    
        // dispatch_group_enter & dispatch_group_leave 必须成对出现
        dispatch_group_enter(group);
        dispatch_group_async(group, q, ^{
            NSLog(@"任务 1 %@", [NSThread currentThread]);
    
            // dispatch_group_leave 必须是 block 的最后一句
            dispatch_group_leave(group);
        });
    
        dispatch_group_enter(group);
        dispatch_group_async(group, q, ^{
            NSLog(@"任务 2 %@", [NSThread currentThread]);
    
            // dispatch_group_leave 必须是 block 的最后一句
            dispatch_group_leave(group);
        });
    
        // 4. 阻塞式等待调度组中所有任务执行完毕
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
        // 5. 判断异步
        NSLog(@"OVER %@", [NSThread currentThread]);
    }
  • 相关阅读:
    Vue表单输入绑定(文本框和复选框)
    Vue登录方式的切换
    IDEA导包(以junit为例)
    反射相关类
    dom4j加载xml文件
    Vue购物车
    IDEA基本设置和快捷键大全
    react组件间的通信-父子通信,子父通信,兄弟通信
    react受控组件
    react中数据承载--props和state用法
  • 原文地址:https://www.cnblogs.com/jiahao89/p/5118280.html
Copyright © 2011-2022 走看看