MrTung / MrTung.github.io

Blog
http://dongxuwei.com
MIT License
12 stars 0 forks source link

葵花宝典之GCD篇 - 董徐维的博客 | Tung's Blog #8

Open MrTung opened 6 years ago

MrTung commented 6 years ago

http://dongxuwei.com/2018/03/12/GCD/

Every failure is leading towards success.

MrTung commented 6 years ago

欢迎各位大佬指正错误,提出问题一起探讨。

MrTung commented 6 years ago

NSLog(@"----start-----当前线程---%@",[NSThread currentThread]);

dispatch_semaphore_t semaphore = dispatch_semaphore_create(3);

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_group_t group =  dispatch_group_create();

dispatch_group_async(group, queue, ^{
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

    NSLog(@"----开始执行第一个任务---当前线程%@",[NSThread currentThread]);

    [NSThread sleepForTimeInterval:2];

    NSLog(@"----结束执行第一个任务---当前线程%@",[NSThread currentThread]);

    dispatch_semaphore_signal(semaphore);

});

dispatch_group_async(group, queue, ^{
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

    NSLog(@"----开始执行第2个任务---当前线程%@",[NSThread currentThread]);

    [NSThread sleepForTimeInterval:2];

    NSLog(@"----结束执行第2个任务---当前线程%@",[NSThread currentThread]);

    dispatch_semaphore_signal(semaphore);

});

dispatch_group_async(group, queue, ^{
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

    NSLog(@"----开始执行第3个任务---当前线程%@",[NSThread currentThread]);

    [NSThread sleepForTimeInterval:2];

    NSLog(@"----结束执行第3个任务---当前线程%@",[NSThread currentThread]);

    dispatch_semaphore_signal(semaphore);

});

dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    [NSThread sleepForTimeInterval:2];

    NSLog(@"----执行最后的汇总任务---当前线程%@",[NSThread currentThread]);
});