遇到一个题

NSLog(@"1");

    

    dispatch_async(dispatch_get_main_queue(), ^{

        

        NSLog(@"2");

    });

    NSLog(@"3");

 

打印结果是  132  

 

把async 换成sync  

 

打印结果是 1  蹦了(线程死锁)

 

 

思考:

dispatch_async中加入dispatch_get_main_queue中,是异步运行中把主队列中的运行任务放到主线程中去运行。

而dispatch_sync中的dispatch_get_main_queue,是在主线程中运行主队列,当把主队列中得任务放到主线程时,会和dispatch_sync进行线程争夺。这时就会产生线程死锁。

 

async 与sync

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-08
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-06-14
  • 2021-12-10
猜你喜欢
  • 2021-08-05
  • 2021-09-27
  • 2021-08-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
相关资源
相似解决方案