GCDマルチスレッドのマルチタスクはすべてのタスクの完了を同時に待つ
2200 ワード
GCDマルチスレッドのマルチタスクはすべてのタスクの完了を同時に待つ
実際のプロジェクトでは、複数のネットワークリクエストを発行し、すべての結果が戻ってから次の操作を行う必要があることがよくあります.ではGCDはこのようなニーズを完璧に解決しました.
1、第一の方式1. //
2. dispatch_group_t group = dispatch_group_create();
4. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
5. sleep(1);
6. NSLog(@" 1");
7. });
8. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
9. sleep(2);
10. NSLog(@" 2");
11. });
12. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
13. sleep(3);
14. NSLog(@" 3");
15. });
17. //
18. dispatch_group_notify(group, dispatch_get_main_queue(), ^{
19. NSLog(@"GCD_1 ");
20. });
21. }
2、第二の方式1. - (void)p_loadGCD_2 {
2. //
3. dispatch_group_t group = dispatch_group_create();
5. //
6. dispatch_group_enter(group);
7. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
8. sleep(1);
9. NSLog(@" 1");
10. //
11. dispatch_group_leave(group);
12. });
13. dispatch_group_enter(group);
14. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
15. sleep(2);
16. NSLog(@" 2");
17. dispatch_group_leave(group);
18. });
19. dispatch_group_enter(group);
20. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
21. sleep(3);
22. NSLog(@" 3");
23. dispatch_group_leave(group);
24. });
26. //
27. dispatch_group_notify(group, dispatch_get_main_queue(), ^{
28. NSLog(@"GCD_2 ");
29. });
30. }
第三の方式:信号量1. - (void)p_loadGCD_3 {
2. //
3. dispatch_group_t group = dispatch_group_create();
4. // 10 semaphore
5. dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
7. for (NSInteger i = 0; i < 10; ++i) {
8. // , 0 , , -1。
9. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
10. dispatch_group_async(group, dispatch_get_main_queue(), ^{
11. NSLog(@" %ld", (long)i);
12. sleep(2);
13. // , , 1
14. dispatch_semaphore_signal(semaphore);
15. });
16. }
1. //
2. dispatch_group_t group = dispatch_group_create();
4. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
5. sleep(1);
6. NSLog(@" 1");
7. });
8. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
9. sleep(2);
10. NSLog(@" 2");
11. });
12. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
13. sleep(3);
14. NSLog(@" 3");
15. });
17. //
18. dispatch_group_notify(group, dispatch_get_main_queue(), ^{
19. NSLog(@"GCD_1 ");
20. });
21. }
1. - (void)p_loadGCD_2 {
2. //
3. dispatch_group_t group = dispatch_group_create();
5. //
6. dispatch_group_enter(group);
7. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
8. sleep(1);
9. NSLog(@" 1");
10. //
11. dispatch_group_leave(group);
12. });
13. dispatch_group_enter(group);
14. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
15. sleep(2);
16. NSLog(@" 2");
17. dispatch_group_leave(group);
18. });
19. dispatch_group_enter(group);
20. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
21. sleep(3);
22. NSLog(@" 3");
23. dispatch_group_leave(group);
24. });
26. //
27. dispatch_group_notify(group, dispatch_get_main_queue(), ^{
28. NSLog(@"GCD_2 ");
29. });
30. }
1. - (void)p_loadGCD_3 {
2. //
3. dispatch_group_t group = dispatch_group_create();
4. // 10 semaphore
5. dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
7. for (NSInteger i = 0; i < 10; ++i) {
8. // , 0 , , -1。
9. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
10. dispatch_group_async(group, dispatch_get_main_queue(), ^{
11. NSLog(@" %ld", (long)i);
12. sleep(2);
13. // , , 1
14. dispatch_semaphore_signal(semaphore);
15. });
16. }