dispatch_group_t

2019-03-20  本文已影响0人  Nulll

要让某些多个事情执行完成后在执行某个事件 dispatch_group
几个函数:

    1)dispatch_group_create()  //创建一个线程组
    2)dispatch_queue_create(<#const char * _Nullable label#>, <#dispatch_queue_attr_t  _Nullable attr#>)  //创建一个线程
    3)dispatch_group_async(<#dispatch_group_t  _Nonnull group#>, <#dispatch_queue_t  _Nonnull queue#>, <#^(void)block#>)  //异步并行执行组合里面的
    4)  dispatch_group_enter(group);
    5)  dispatch_group_leave(group);
    6)dispatch_group_notify(<#dispatch_group_t  _Nonnull group#>, <#dispatch_queue_t  _Nonnull queue#>, <#^(void)block#>)  //执行完成后通知需要做的事

实际代码:

    dispatch_group_t group = dispatch_group_create();
    
    dispatch_queue_t queue = dispatch_queue_create("dispatch_group", DISPATCH_QUEUE_CONCURRENT);
    
    NSLog(@"aaaaaa");
    
    dispatch_group_async(group, queue, ^{
        for (int i = 0; i < 1000000; i++) {
            
        }
        NSLog(@"test1");
    });
    
    NSLog(@"bbbbbb");
    dispatch_group_async(group, queue, ^{
        NSLog(@"test2");
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"test3");
    });
    NSLog(@"cccccc");
    

dispatch_group_enter(group);
//异步执行完成后
dispatch_group_leave(group);

    
    dispatch_group_notify(group, queue, ^{
        NSLog(@"down....");
    });

执行效果:

屏幕快照 2019-03-20 下午5.44.57.png
上一篇下一篇

猜你喜欢

热点阅读