1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| GCD方法: //创建信号量 __block dispatch_semaphore_t test = dispatch_semaphore_create(0); //创建队列 dispatch_queue_t queue = dispatch_queue_create("testBlock", NULL); dispatch_async(queue, ^{ for (int i = 0; i< 4; i++) { NSLog(@"i的值是:%d",i); tempStr = [NSString stringWithFormat:@"%d",i]; } //发送通知 dispatch_semaphore_signal(test);
}); //信号量等待 dispatch_semaphore_wait(test, DISPATCH_TIME_FOREVER); if([tempStr isEqualToString:@"5"]) { for (int j = 0; j<5; j++) { NSLog(@"j的值是:%d",j); } }
|