GCD线程间通信


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // [self downloadRequestOne];
    // [self downloadRequestTwo];
    // [self downloadRequestThree];
    // [self downloadRequestFour];
    
    [self downloadRequestFive];

}

- (void)downloadRequestFive
{
    dispatch_queue_t queueOne = dispatch_queue_create(0, DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queueTwo = dispatch_get_global_queue(0, 0);
    
    dispatch_group_t groupD = dispatch_group_create();
    
    dispatch_group_enter(groupD);
    dispatch_group_async(groupD, queueOne, ^{
        dispatch_async(queueTwo, ^{
            
            sleep(3);
            NSLog(@"任务一完成");
            dispatch_group_leave(groupD);
        });
    });
    
    dispatch_group_enter(groupD);
    dispatch_group_async(groupD, queueOne, ^{
        dispatch_async(queueTwo, ^{
            sleep(5);
            NSLog(@"任务二完成");
            dispatch_group_leave(groupD);
        });
    });
    
    dispatch_group_notify(groupD, dispatch_get_main_queue(), ^{
        
        NSLog(@"任务都完成了");
        
    });
    
    /*
     
     *  如果dispatch_group_async里执行的是异步代码dispatch_group_notify会直接触发而不会等待异步任务完成,
     而dispatch_group_enter、和dispatch_group_leave则不会有这个问题,它们只需要在任务开始前enter结束后
     leave即可达到线程同步的效果。
     
     2017-04-07 17:58:12.388 test0407GCD[62203:70999094] 任务一完成
     2017-04-07 17:58:14.390 test0407GCD[62203:70999122] 任务二完成
     2017-04-07 17:58:14.391 test0407GCD[62203:70999026] 任务都完成了
     
     */
}


- (void)downloadRequestFour
{
 
    dispatch_queue_t queueOne = dispatch_get_global_queue(0, 0);
    dispatch_queue_t queueTwo = dispatch_queue_create("AAAAA", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_group_t groupC = dispatch_group_create();
    
    dispatch_group_async(groupC, queueOne, ^{
        dispatch_async(queueTwo, ^{
            
            sleep(3);
            NSLog(@"任务一完成");
        });
    });
    
    dispatch_group_async(groupC, queueOne, ^{
        dispatch_async(queueTwo, ^{
            sleep(5);
            NSLog(@"任务二完成");
        });
    });
    
    dispatch_group_notify(groupC, dispatch_get_main_queue(), ^{
        
        NSLog(@"任务都完成了");
    });
    
    /*
     
     *  如果dispatch_group_async里执行的是异步代码dispatch_group_notify会直接触发而不会等待异步任务完成,
        而dispatch_group_enter、和dispatch_group_leave则不会有这个问题,它们只需要在任务开始前enter结束后
        leave即可达到线程同步的效果。
     
     2017-04-07 17:44:04.154 test0407GCD[62176:70983482] 任务都完成了
     2017-04-07 17:44:07.149 test0407GCD[62176:70983611] 任务一完成
     2017-04-07 17:44:09.148 test0407GCD[62176:70983608] 任务二完成
     
     */
    
}

- (void)downloadRequestThree
{
    
    dispatch_group_t groupB = dispatch_group_create();
    
    dispatch_group_enter(groupB);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        sleep(3);
        NSLog(@"任务一完成");
        dispatch_group_leave(groupB);
    });
    
    dispatch_group_enter(groupB);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        sleep(5);
        NSLog(@"任务二完成");
        dispatch_group_leave(groupB);
    });
    
    dispatch_group_notify(groupB, dispatch_get_main_queue(), ^{
        
        NSLog(@"任务都完成了");
    });
    
    /*
     
     2017-04-07 17:35:32.115 test0407GCD[62138:70954764] 任务一完成
     2017-04-07 17:35:34.114 test0407GCD[62138:70954766] 任务二完成
     2017-04-07 17:35:34.115 test0407GCD[62138:70954722] 任务都完成了
     
     */
    
}

- (void)downloadRequestTwo
{
    
    dispatch_queue_t queueOne = dispatch_get_global_queue(0, 0);
    
    dispatch_queue_t queueTwo = dispatch_queue_create("AAAAA", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_group_t groupA = dispatch_group_create();
    
    dispatch_group_async(groupA, queueOne, ^{
        
        sleep(3);
        NSLog(@"任务一完成");
    });
    
    dispatch_group_async(groupA, queueOne, ^{
        
        sleep(6);
        NSLog(@"任务二完成");
    });
    
    dispatch_group_async(groupA, queueTwo, ^{
        
        sleep(9);
        NSLog(@"任务三完成");
    });
    
    dispatch_group_notify(groupA, dispatch_get_global_queue(0, 0), ^{
       
        NSLog(@"任务都完成了");
    });
    
    
    /*
     
     2017-04-07 17:24:12.907 test0407GCD[62055:70916330] 任务一完成
     2017-04-07 17:24:15.909 test0407GCD[62055:70916328] 任务二完成
     2017-04-07 17:24:18.909 test0407GCD[62055:70916327] 任务三完成
     2017-04-07 17:24:18.909 test0407GCD[62055:70916327] 任务都完成了
     
     */
    
}

- (void)downloadRequestOne
{
    
    dispatch_queue_t queueOne = dispatch_queue_create("AAAAA", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t groupOne = dispatch_group_create();
    
    dispatch_group_async(groupOne, queueOne, ^{
        
        NSLog(@"任务一");
    });
    
    dispatch_group_async(groupOne, queueOne, ^{
       
        sleep(8);
        NSLog(@"任务二");
    });
    
    
    dispatch_group_notify(groupOne, queueOne, ^{
       
        NSLog(@"dispatch_group_notify");
        NSLog(@"Notify Execute");
    });
    
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        dispatch_group_wait(groupOne, dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC));
        NSLog(@"dispatch_group_wait");
        NSLog(@"等待超过5秒,等待超时");
    });
    
    
    /*
     
     2017-04-07 16:45:30.577 test0407GCD[61628:70780420] 任务一
     
     2017-04-07 16:45:35.578 test0407GCD[61628:70780423] dispatch_group_wait
     2017-04-07 16:45:35.579 test0407GCD[61628:70780423] 等待超过5秒,等待超时
     
     2017-04-07 16:45:38.579 test0407GCD[61628:70780421] 任务二
     
     2017-04-07 16:45:38.579 test0407GCD[61628:70780423] dispatch_group_notify
     2017-04-07 16:45:38.580 test0407GCD[61628:70780423] Notify Execute

     */
    
    
    /*
     *  当Group中的任务都完成时,该Group的 group_wati方法会结束等待
     
     
     2017-04-07 17:04:10.863 test0407GCD[61886:70846523] 任务一
     
     2017-04-07 17:04:18.868 test0407GCD[61886:70846524] 任务二
     
     2017-04-07 17:04:18.869 test0407GCD[61886:70846524] dispatch_group_notify
     
     2017-04-07 17:04:18.869 test0407GCD[61886:70846528] dispatch_group_wait
     2017-04-07 17:04:18.870 test0407GCD[61886:70846528] 等待超过10秒,等待超时
     
     2017-04-07 17:04:18.870 test0407GCD[61886:70846524] Notify Execute
     
     */
    
}


@end

你可能感兴趣的:(GCD线程间通信)