美文网首页
dispatch_group示例

dispatch_group示例

作者: 迷路的安然和无恙 | 来源:发表于2018-04-16 23:12 被阅读8次

应用场景

  • 当前线程依赖其他线程的执行结果
  • 需要依赖多个线程执行完成后的结果,并及时处理
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_queue_t concurrentQueue = dispatch_queue_create("custemer_queue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_group_async(group, concurrentQueue, ^{
        NSLog(@"A");
    });
    dispatch_group_async(group, concurrentQueue, ^{
        NSLog(@"B");
    });
    dispatch_group_async(group, concurrentQueue, ^{
        NSLog(@"C");
    });
    dispatch_group_async(group, concurrentQueue, ^{
        NSLog(@"D");
    });
    
    dispatch_group_notify(group, concurrentQueue, ^{
        NSLog(@"都执行完了");
    });
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
    NSLog(@"finish");
}

image.png

相关文章

网友评论

      本文标题:dispatch_group示例

      本文链接:https://www.haomeiwen.com/subject/sbuykftx.html