1.首先看一下官方文档的解释,这个block的队列是同步执行的,不像异步,这个方法直到block执行完毕才会返回异步
2.主线程一旦开启,就要先把本身的代码执行完成以后,才去执行加入到主队列中的任务async
void dispatch_sync( dispatch_queue_t queue, dispatch_block_t block);
this
queue |
The queue on which to submit the block. This parameter cannot be |
block |
The block to be invoked on the target dispatch queue. This parameter cannot be |
Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async
, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.code
a) dispatch_sync这个方法要等到block的执行完以后,才返回orm
b) 主线程一旦开启,就要先把本身的代码执行完成以后,才去执行加入到主队列中的任务队列
c) 这样主线程想要执行block,先要下去执行下面的代码,可是由于dispatch_sync这个方法,,若是返回不了,主线程就没法向下再次执行,因此形成了死锁,因此把主线程卡到这里了,形成了死锁ci
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"1"); // 任务1 此时只会执行到任务一结束,就不会再继续执行了 dispatch_sync(dispatch_get_main_queue(), ^{ NSLog(@"2--%@",[NSThread currentThread]); // 任务2 }); NSLog(@"3"); // 任务3 }
NSLog(@"1"); // 任务1 dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSLog(@"2"); // 任务2 }); NSLog(@"3"); // 任务3 此时都会打印出来