dispatch_group_notify方法调用执行时,如果没有添加到group的任务在执行,那么直接执行notify的Block,如果有则等待现有的任务执行完之后执行notify的Block
- (void)test_1 { dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_queue_create("test_for_group_events", DISPATCH_QUEUE_CONCURRENT); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"=========> finsh running 00"); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"=========> finsh running 01"); }); dispatch_group_async(group, queue, ^{ [NSThread sleepForTimeInterval:1.0]; NSLog(@"============> 1"); }); dispatch_group_enter(group); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:0.6]; NSLog(@"============> 2"); dispatch_group_leave(group); }); dispatch_group_notify(group, queue, ^{ NSLog(@"=========> finsh running 02"); }); dispatch_group_enter(group); dispatch_async(dispatch_get_main_queue(), ^{ [NSThread sleepForTimeInterval:0.3]; NSLog(@"============> 3"); dispatch_group_leave(group); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"=========> finsh running 03"); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"=========> finsh running 04"); }); }
给dispatch_queue增加属性
dispatch_queue_set_specific(_queue, kDispatchQueueSpecificKey, (__bridge void *)self, NULL);
dispatch_sync(dispatch_get_global_queue(0, 0), ^{ NSLog(@"是否在主线程执行:%d",[NSThread isMainThread]); NSLog(@"%@",[NSOperationQueue currentQueue]); });
在主线程调用同步,输出为是主线程、为主队列