dispatch_async 和 dispatch_sync的区别

dispatch_async   异步执行

dispatch_sync     同步执行

 

Stack Overflow 上有很好的解释:

Yes,Using serial queue ensure the serial execution of tasks.The only difference is that dispatch_sync only return after the block is finished whereas dispatch_async return after it is added to the queue and may not finished.

for this code

dispatch_async(_serialQueue, ^{ printf("1");});
printf("2");
dispatch_async(_serialQueue, ^{ printf("3"); });
printf("4");

 It may print 2413 or 2143 or 1234 but 1 always before 3

for this code

dispatch_sync(_serialQueue, ^{ printf("1");});
printf("2");
dispatch_sync(_serialQueue, ^{ printf("3"); });
printf("4");

 it always print 1234

 

posted @ 2015-11-26 16:56  LaiSong  阅读(255)  评论(0编辑  收藏  举报