Open MrTung opened 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]);
});
http://dongxuwei.com/2018/03/12/GCD/
Every failure is leading towards success.