dispatch_suspend用于挂起队列,dispatch_resume用于恢复队列,但是从这两个方法的传参来看,计时器或者其他dispatch_object_t都是可以的。
队列挂起后,已经在执行的任务无法停止,但是没开始的任务是会等待resume后才开始执行。但是经过测试发现,系统提供的dispatch_get_global_queue获取的并行队列并不受到挂起和恢复的影响
- (void)func08
{
dispatch_queue_t queue = dispatch_queue_create("MYQUEUE", DISPATCH_QUEUE_SERIAL);
// dispatch_queue_t queue = dispatch_queue_create("MYQUEUE", DISPATCH_QUEUE_CONCURRENT);
// dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"0: == %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
}
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"挂起队列");
dispatch_suspend(queue);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"插入任务");
dispatch_async(queue, ^{
for (NSInteger i = 0; i < 10; i++) {
NSLog(@"1: == %@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
}
});
NSLog(@"插入完成");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(12 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"恢复队列");
dispatch_resume(queue);
});
}
网友评论