dispatch_group示例

应用场景

  • 当前线程依赖其他线程的执行结果
  • 需要依赖多个线程执行完成后的结果,并及时处理
- (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示例)